Javascript 正则表达式中需要插入符号(^)和美元符号($)是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5921699/
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
What is the need for caret (^) and dollar symbol ($) in regular expression?
提问by Ant's
I have read recently about Java Script regular expressions, but i have got confused.
My author says that it is must to include the caret (^
) and dollar symbol ($
) at the beginning and end of the all regular expressions declarations? Why this is needed actually? Whats its purpose?
我最近阅读了有关 Java Script 正则表达式的内容,但我感到困惑。
我的作者说必须在所有正则表达式声明的开头和结尾包含脱字符号 ( ^
) 和美元符号 ( $
) 吗?为什么实际上需要这样做?它的目的是什么?
Correct me if am wrong!
如果错了请纠正我!
采纳答案by Eric Wendelin
Javascript RegExp() allows you to specify a multi-line mode (m) which changes the behavior of ^
and $
.
JavaScript的正则表达式()允许您指定多行模式(M),其改变的行为^
和$
。
^
represents the start of the current line in multi-line mode, otherwise the start of the string
^
表示多行模式下当前行的开始,否则为字符串的开始
$
represents the end of the current line in multi-line mode, otherwise the end of the string
$
表示多行模式下当前行的结束,否则为字符串的结束
For example: this allows you to match something like semicolons at the end of a line where the next line starts with "var" /;$\n\s*var/m
例如:这允许您在下一行以“var”开头的行末尾匹配分号之类的内容 /;$\n\s*var/m
Fast regexen also need an "anchor" point, somewhere to start it's search somewhere in the string. These characters tell the Regex engine where to start looking and generally reduce the number of backtracks, making your Regex much, much fasterin many cases.
快速正则表达式还需要一个“锚”点,在某个地方开始搜索字符串中的某个位置。这些字符告诉 Regex 引擎从哪里开始查找,并且通常会减少回溯的数量,在许多情况下使您的 Regex快得多。
NOTE: This knowledge came from Nicolas Zakas's High Performance Javascript
注意:这些知识来自 Nicolas Zakas 的High Performance Javascript
Conclusion: You should use them!
结论:你应该使用它们!
回答by Rudie
^
represents the start of the input string.
^
表示输入字符串的开始。
$
represents the end.
$
代表结束。
You don't actually have to use them at the start and end. You can use em anywhere =) Regex is fun (and confusing). They don't represent a character. They represent the start and end.
您实际上不必在开始和结束时使用它们。您可以在任何地方使用 em =) 正则表达式很有趣(而且令人困惑)。它们不代表一个字符。他们代表开始和结束。
回答by Oded
They match the start of the string (^
) and end of the string ('$').
它们匹配字符串的开头 ( ^
) 和字符串的结尾 ('$')。
You should use them when matching strings at the start or end of the string. I wouldn't say you haveto use them, however.
您应该在匹配字符串开头或结尾的字符串时使用它们。但是,我不会说您必须使用它们。
回答by Donal Fellows
^
anchors the beginning of the RE at the start of the test string, and $
anchors the end of the RE at the end of the test string. If that's what you want, go for it! However, if you're using REs of the form ^.*theRealRE.*$
then you might want to consider dropping the anchors and just using the core of the RE on its own.
^
将 RE 的开头锚定在测试字符串的开头,并将 RE$
的结尾锚定在测试字符串的末尾。如果这就是你想要的,那就去吧!但是,如果您正在使用这种形式的 RE,^.*theRealRE.*$
那么您可能需要考虑删除锚点并仅使用 RE 的核心本身。
Some languages force REs to be anchored at both ends by default.
默认情况下,某些语言强制 RE 锚定在两端。
回答by ???
I have tested these.
1. /^a/ matches abb, ab but not ba, bab, bba.
2. /a/ matches abb, ab and ba, bab, bba.
我已经测试了这些。
1. /^a/ 匹配 abb, ab 但不匹配 ba, bab, bba。
2. /a/ 匹配 abb, ab 和 ba, bab, bba。
I think that /^a/ matches such strings starting a.
/a/ matches such strings contains a.
我认为 /^a/ 匹配以 a 开头的字符串。
/a/ 匹配包含 a 的此类字符串。
Similar to /^a/, /a$/ matches ba, a, but not ab, bab.
与 /^a/ 类似,/a$/ 匹配 ba, a,但不匹配 ab, bab。
Refer http://www.regular-expressions.info/anchors.html.
请参阅http://www.regular-expressions.info/anchors.html。
If you notify wrong(or strange) sentence in above or this to me, I would thank you.
如果你通知我上面或这个错误(或奇怪)的句子,我会感谢你。