javascript 用正则表达式在等号后显示文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8528873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing the text after an equal sign with regexp?
提问by Shawn31313
I would like to know if there is a code, preferable Regexp, that can get all the text after an equal sign.
我想知道是否有一个代码,更可取的 Regexp,可以在等号后获取所有文本。
For example:
例如:
3+4=7
3+4=7
Results:
结果:
7
7
Is this even possible? I hope so, thanks in advance.
这甚至可能吗?我希望如此,提前致谢。
回答by Jo?o Silva
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);
}
Working example in jsfiddle.
jsfiddle 中的工作示例。
回答by j?rgensen
/=(.*)/
should suffice, since it will find a result on the first =.
/=(.*)/
应该足够了,因为它会在第一个 = 上找到结果。
Other possibilities (can be transcribed into languages other than Perl too)
其他可能性(也可以转录成 Perl 以外的语言)
$x = "foo=bar";
print "$'" if $x =~ /(?<==)/; # $' = that after the matched string
print "$&" if $x =~ /(?<==).*/; # $& = that which matched
print "" if $x =~ /=(.*)/; # first suggestion from above