javascript 在javascript正则表达式中选择任何符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8940553/
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
Choose any symbol in javascript regex
提问by Olga
I have a string containing something like this
我有一个包含类似内容的字符串
"... /\*start anythingCanBeEnteredHere end\*/ ..."
I need a regex that gets only the anythingCanBeEnteredHere
part, which can be a collection of any number of symbols.
我需要一个只获取anythingCanBeEnteredHere
部分的正则表达式,它可以是任意数量符号的集合。
The problem is that I can't find any shortcut/flag that will choose any symbol
问题是我找不到任何可以选择任何符号的快捷方式/标志
So far I use this regex
到目前为止,我使用这个正则表达式
var regex = /start([^\~]*)end/;
var templateCode = myString.match(regex);
[^\~]
chooses any symbol except "~"
(which is a hack) and works fine, but I really need all symbols.
[^\~]
选择除"~"
(这是一个黑客)之外的任何符号并且工作正常,但我真的需要所有符号。
I've also tried this [^]
but it doesn't work right.
我也试过这个,[^]
但它不起作用。
回答by georg
/start(.*)end/
will match FOO
in startFOOend
and BARendBAZ
in startBARendBAZend
.
将匹配FOO
instartFOOend
和BARendBAZ
in startBARendBAZend
。
/start(.*?)end/
will match FOO
in startFOOend
and BAR
in startBARendBAZend
.
将匹配FOO
instartFOOend
和BAR
in startBARendBAZend
。
The dot matches anything except a newline symbol (\n
). If you want to capture newlines as well, replace dot with [\s\S]
. Also, if you don't allow the match to be empty (as in startend
), use +
instead of *
.
点匹配除换行符 ( \n
)之外的任何内容。如果您还想捕获换行符,请将 dot 替换为[\s\S]
. 此外,如果您不允许匹配为空(如startend
),请使用+
代替*
。
See http://www.regular-expressions.info/reference.htmlfor more info.
有关更多信息,请参阅http://www.regular-expressions.info/reference.html。
回答by Sam Greenhalgh
I'm not sure I understand what you mean by "symbol", if you mean anything, that's what the dot .
will match
我不确定我是否理解你所说的“符号”是什么意思,如果你有什么意思,那就是点.
匹配的意思
Are you trying to do this?
你想这样做吗?
var regex = /start(.*)end/;
var templateCode = myString.match(regex);