[英]Javascript Regular Expression excluding a word
i am familiar with javascript regular expression to not match a word but it does not help me much. 我熟悉javascript正则表达式,无法匹配单词,但对我没有太大帮助。 when given a string (with any type of characters), i wish to parse it around two tokens, "//" and "\\\\".
当给定一个字符串(带有任何类型的字符)时,我希望围绕两个标记“ //”和“ \\\\”解析它。 i did the following:
我做了以下事情:
var patt = /.*[^"//"]/gm;
patt.exec(str);
but it seems to match any occurrences of the characters between the quotes, ie "/" and "//". 但它似乎匹配引号之间所有出现的字符,即“ /”和“ //”。 how may i achieve it?
我该如何实现?
When you start a character class with ^
(as you have in [^"//"]
), it means "any character except the ones listed." 当您使用
^
开头字符类时(就像[^"//"]
字符一样),它的意思是“除列出的字符外的任何字符”。 So [^"//"]
means "match one of any character except "
and /
(it ignores the fact you've listed each of them twice). 因此
[^"//"]
意思是“匹配除"
和/
以外的任何字符之一(它忽略了您两次列出每个字符的事实)。
If you're trying to match the text between two slashes ( //
) and one backslash ( \\
) (as per your question; you later made a comment suggesting it's two backslashes, I address that later), then: 如果您要使两个斜杠(
//
)和一个反斜杠( \\
)之间的文本匹配(根据您的问题;您稍后发表评论建议它是两个反斜杠,我稍后再解决),然后:
var match = str.match(/\/\/(.*?)\\/); // Match between // and \
Note that we have to escape the slashes because the slash is the regular expression delimiter; 注意,我们必须转义斜杠,因为斜杠是正则表达式定界符。 and we have to escape backslashes because the backslash is the escape character.
并且我们必须转义反斜杠,因为反斜杠是转义字符。
The above means "match two slashes followed by zero or more of any character followed by a backslash." 上面的意思是“匹配两个斜杠,后跟任何字符的零个或多个,后跟一个反斜杠。” The
?
?
after *
makes *
non-greedy (so it will consume as few characters as it can to satisfy the expression). 在
*
之后使*
非贪心(因此它将消耗尽可能少的字符以满足表达式)。 The ()
create a capture group , which in the match object will receive the characters that matched in that position. ()
创建一个捕获组 ,它在match对象中将接收在该位置匹配的字符。
Example: 例:
test("foo");
test("foo //bar");
test("foo //bar\\");
test("foo //bar\\ baz");
test("bar\\ baz");
test("//bar\\ baz");
test("foo //bar\\ baz \\ more \\ more");
function test(str) {
var m = str.match(/\/\/(.*?)\\/),
cap = (m && m[1]) || "<em>nothing</em>";
display("Str: <code>" + str + "</code>: Captured <code>" + cap + "</code>");
}
Output: 输出:
Str:
foo
: Capturednothing
Str:
foo
:nothing
捕获Str:
foo //bar
: Capturednothing
Str:
foo //bar
:nothing
捕获Str:
foo //bar\\
: Capturedbar
Str:
foo //bar\\
:捕获的bar
Str: foo
//bar\\ baz
: Capturedbar
Str:foo
//bar\\ baz
:捕获的bar
Str:
bar\\ baz
: Capturednothing
Str:
bar\\ baz
:未捕获nothing
Str:
//bar\\ baz
: Capturedbar
Str:
//bar\\ baz
:捕获的bar
Str:
foo //bar\\ baz \\ more \\ more
: Capturedbar
Str:
foo //bar\\ baz \\ more \\ more
:捕获的bar
Or for two backslashes: 或两个反斜杠:
var match = str.match(/\/\/(.*?)\\\\/); // Match between // and \\
Live copy (the output is the same, just with two backslashes) 实时副本 (输出是相同的,只是两个反斜杠)
Some reading on JavaScript regular expressions: 有关JavaScript正则表达式的一些阅读:
RegExp
page RegExp
页面 怎么样
var tokens = str.split(/\/\/|\\\\/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.