2011-12-16 185 views
4

我想知道是否有代碼,更好的Regexp,可以在等號後獲得所有文本。用正則表達式顯示等號後的文本?

例如:

3 + 4 = 7

結果:

這甚至可能?我希望如此,提前感謝。

+2

爲什麼最好使用正則表達式?只需在`「=」`分開。無論如何,你需要說出你正在使用的語言;有差異。 – 2011-12-16 01:58:41

+0

這是行不通的。 – Shawn31313 2011-12-16 02:06:14

+0

如果你說*爲什麼*它不起作用會更有用,因爲顯然你提出這個問題的方式,它會完美地工作,並且更容易。 – 2011-12-16 02:25:26

回答

6
var s = "3+4=7"; 
var regex = /=(.+)/; // match '=' and capture everything that follows 
var matches = s.match(regex); 
if (matches) { 
    var match = matches[1]; // captured group, in this case, '7' 
    document.write(match); 
} 

工作示例的jsfiddle

0

/=(.*)/應該就夠了,因爲它會在first =上找到結果。

其他可能性(可以轉錄成比Perl過其他語言)

$x = "foo=bar"; 
print "$'" if $x =~ /(?<==)/; # $' = that after the matched string 
print "$&" if $x =~ /(?<==).*/; # $& = that which matched 
print "$1" if $x =~ /=(.*)/; # first suggestion from above