2

10PL2 つの正規表現が必要です。これは、 isで終わるものすべてに一致し(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));

結果はすべて null でした (document.write は何も書き込みませんでした)。では、これら2つのパターンの正規表現を理解するのを手伝ってくれる人はいますか?

4

2 に答える 2

2

パターンに構文エラーがあります-/正規表現の真ん中は

  • 必要ありません (一致させたいリテラル スラッシュはありませんか?)、および
  • 正規表現エンジンはそれを終了正規表現区切り文字として解釈するため、混乱を招きます。次に、正規表現修飾子として解釈しようとしますPL/g(もちろん機能しません)。

また、行末でのみそのパターンに一致する必要があることを正規表現に伝えていません。だからこれを試してください:

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

$(修飾子と共にm) 行末に一致します。その修飾子がないと、文字列の末尾にのみ一致します。

2 つ目の場合:

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

また、しないでくださいvisit W3Schools。インターネット上で最もエラーが多い場所の 1 つです。信じられない場合は、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 に答える