2

我需要两个正则表达式,它匹配任何以10PLis结尾的东西(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 没有写任何东西)。那么,谁能帮我找出这两种模式的正则表达式?

4

2 回答 2

2

您的模式中有语法错误 -/正则表达式的中间是

  • 不需要(没有要匹配的文字斜线,是吗?),和
  • 使正则表达式引擎感到困惑,因为它将其解释为结束正则表达式分隔符。然后它尝试解释PL/g为正则表达式修饰符(这当然不起作用)。

此外,您并没有告诉正则表达式它应该只在行尾匹配该模式。所以试试这个:

var patt1 = / \d+PL$/gm;

$匹配(与m修饰符一起)行尾。如果没有那个修饰符,它只会匹配字符串的结尾。

对于第二个:

var patt2 = / \d+(?:\.\d+)?X\d+(?:\.\d+)?$/gm;

另外,请不要visit W3Schools。这是互联网上最容易出错的地方之一。如果您不相信我,请查看http://w3fools.com

于 2012-12-01T05:46:24.270 回答
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
于 2012-12-01T05:54:52.323 回答