2012-12-01 76 views
2

我需要兩個正則表達式,以匹配任何以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

[這可能是有幫助的(http://www.regular-expressions.info/javascriptexample.html) – steveax

回答

2

您的模式中的語法錯誤 - 在正則表達式的中間/

  • 沒有必要(也就是你想匹配沒有文字斜槓,是不是?),並
  • 對正則表達式引擎感到困惑,因爲它將其解釋爲結束正則表達式分隔符。然後它試圖將PL/g解釋爲正則表達式修飾符(這當然不起作用)。

此外,你不告訴正則表達式它只應該在一行的末尾匹配該模式。所以,試試這個:

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

$比賽(與m改性劑一起)行的末尾。沒有該修飾符,它只會匹配字符串的末尾。

對於第二個:

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

而且,請不要visit W3Schools。這是互聯網上錯誤最多的地方之一。如果你不相信我,請查看http://w3fools.com

+0

感謝您的w3fools鏈接! +1 – Razort4x

+0

我有點想把它當作垃圾郵件。 – ddoxey

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