I need two regular expressions, on that matches anything that ends with 10PL that is (a single whitespace) (any integer) (the string PL in caps) (line terminates). I tried the following but none of them works. For eg.
var str="Visit W3Schools 45PL"; // should match this
var str1="Visit W3Schools 45PL Other"; // should NOT match this
var str2 = "Any Random value 133PL" // should match this
var str3 = "Any Random value 133Pl" // should NOT match this
The other should match 21.323X230 (a single whitespace) (any floating value) (the word X in caps) (any other floating point value) (line terminates). For eg.
var test="Visit W3Schools 4X5"; // should match this
var test1="Visit W3Schools 4X5PL Other"; // should NOT match this
var test2 = "Any Random value 13.270X46.96" // should match this
var test3 = "Any Random value 13.21X12.36 " // should NOT match this, as last word is white space
I tried the following pattern (for the first one (the PL one)).
var patt1=/\s+\d+/PL/g;
var patt2 = /[ ]+[0-9]+/PL/g;
document.write(patt1.test(str));
document.write(patt2.test(str));
document.write(patt1.test(str1));
document.write(patt2.test(str1));
document.write(patt1.test(str2));
document.write(patt2.test(str2));
document.write(patt1.test(str3));
document.write(patt2.test(str3));
result was all null (document.write didn’t wrote anything). So, can anyone help me figure out regex for these two patterns?
You have a syntax error in your pattern – the
/in the middle of the regex isPL/gas regex modifiers (which of course doesn’t work).Also, you’re not telling the regex that it should match that pattern only at the end of a line. So try this:
$matches (together with themmodifier) the end of a line. Without that modifier, it would only match the end of the string.For the second one:
Also, please don’t
visit W3Schools. It’s one of the most error-ridden places on the internet. If you don’t believe me, check out http://w3fools.com.