我需要两个正则表达式,匹配任何以10PL
结尾的(a single whitespace) (any integer) (the string PL in caps) (line terminates)
。我尝试了以下但没有一个有效。例如。
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
其他应与21.323X230
(a single whitespace) (any floating value) (the word X in caps) (any other floating point value) (line terminates)
匹配。例如。
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
我尝试了以下模式(第一个模式(PL一个))。
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));
结果全为空(document.write没有写任何东西)。那么,任何人都可以帮我找出这两种模式的正则表达式吗?
答案 0 :(得分:2)
您的模式中存在语法错误 - 正则表达式中间的/
是
PL/g
解释为正则表达式修饰符(当然这不起作用)。另外,你并没有告诉正则表达式它只应该在一行的末尾匹配该模式。所以试试这个:
var patt1 = / \d+PL$/gm;
$
匹配(与m
修饰符一起)一行的结尾。如果没有该修饰符,它只会匹配字符串的结尾。
对于第二个:
var patt2 = / \d+(?:\.\d+)?X\d+(?:\.\d+)?$/gm;
另外,请不要visit W3Schools
。这是互联网上最错误的地方之一。如果您不相信我,请查看http://w3fools.com。
答案 1 :(得分:1)
这应该这样做。
var str = "Visit W3Fools 45PL"; // should match this
var str1 = "Visit W3Fools 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
var test = "Visit W3Fools 4X5"; // should match this
var test1 = "Visit W3Fools 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
var patt1 = /\s\d+PL$/;
document.write( patt1 + ' =~ "' + str + '" => ' + patt1.test(str) + '<br>');
document.write( patt1 + ' =~ "' + str1 + '" => ' + patt1.test(str1) + '<br>');
document.write( patt1 + ' =~ "' + str2 + '" => ' + patt1.test(str2) + '<br>');
document.write( patt1 + ' =~ "' + str3 + '" => ' + patt1.test(str3) + '<br>');
var patt2 = /\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/;
document.write( patt2 + ' =~ "' + test + '" => ' + patt2.test(test) + '<br>');
document.write( patt2 + ' =~ "' + test1 + '" => ' + patt2.test(test1) + '<br>');
document.write( patt2 + ' =~ "' + test2 + '" => ' + patt2.test(test2) + '<br>');
document.write( patt2 + ' =~ "' + test3 + '" => ' + patt2.test(test3) + '<br>');
该页面写为:
/\s\d+PL$/ =~ "Visit W3Fools 45PL" => true
/\s\d+PL$/ =~ "Visit W3Fools 45PL Other" => false
/\s\d+PL$/ =~ "Any Random value 133PL" => true
/\s\d+PL$/ =~ "Any Random value 133Pl" => false
/\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Visit W3Fools 4X5" => true
/\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Visit W3Fools 4X5PL Other" => false
/\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Any Random value 13.270X46.96" => true
/\s\d+(?:[.]\d+)?X\d+(?:[.]\d+)?$/ =~ "Any Random value 13.21X12.36 " => false