[英]JavaScript regex match anything except a letter
I need to match the specific string that comes after "testing" 我需要匹配“测试”之后的特定字符串
Like this 像这样
testing rest -> matches (rest)
testing what -> matches (what)
testing Loong -> matches (Loong)
testing N -> matches (N)
testing L -> this is not matched
testing LL -> matches (LL)
testing J -> matches (J)
testing -> this is not matched
testing -> this is not matched
testing L TY -> this specific string will not occur so it is irrelevant
and with quotes 并带有引号
"testing rest" -> matches (rest)
"testing what" -> matches (what)
"testing Loong" -> matches (Loong)
"testing N" -> matches (N)
"testing L" -> this is not matched
"testing LL" -> matches (LL)
"testing J" -> matches (J)
"testing" -> this is not matched
"testing "-> this is not matched
"testing L TY" -> this specific string will not occur so it is irrelevant
How could I do that? 我该怎么办?
This should do it: 应该这样做:
/^testing ([^L]|..+)$/
or, if You can not remove quotation marks before matching: 或者,如果您不能在匹配之前删除引号:
/^"?testing ([^L"]|.[^"]+)"?$/
Explaination: 说明:
First part: ^testing searches for constant element of Your strings - this is easy part. 第一部分: ^ testing搜索字符串的常量元素-这很容易。
Then, there is atomic group (in round brackets): [^L]|..+ which consists of OR statement (a pipe). 然后,有一个原子组 (在圆括号中): [^ L] | .. + ,它由OR语句(一个管道)组成。
On the left side of this OR we have a search pattern for all one character strings (except for letter " L "). 在该OR的左侧,我们为所有一个字符串(字母“ L ”除外)提供了搜索模式。 It is done be defining set (using square brackets [] ), and negation (using this sign: ^ , which means negation when it is first sign in square brackets).
它是通过定义set(使用方括号[] )和取反(使用此符号^ ,即在方括号中的第一个符号表示否定)来完成的。
On the right side we have search pattern for anything that is at lease two chars long. 在右侧,我们可以搜索长度至少为两个字符的任何内容。 This is done by fisrt matching anything (using dot . ) and then again anything, this time at least once (by using plus sign: + ).
这是通过fisrt匹配所有内容(使用点。 ),然后再匹配任何内容(至少一次)(使用加号: + )来完成的。
Summing this together we should get exactly the kind of logic You requested. 总结一下,我们应该完全得到您所要求的逻辑。
I suggest a lookahead based regex to fail the match if "testing " is followed with L
and 0+ whitespaces before the end of the string: 如果在字符串末尾前加上“
L
和0+空格,则“基于测试”的正则表达式会导致匹配失败:
/^"?testing\s+((?!L?\s*"?\s*$).*?)"?$/
See the regex demo 见正则表达式演示
Details : 详细资料 :
^
- start of string ^
-字符串开头 "?
- an optional "
"?
-可选的"
testing
- a literal string testing
testing
-文字字符串testing
\\s+
- 1 or more whitespaces \\s+
-1个或多个空格 ((?!L?\\s*"?\\s*$).*?)
- Group 1 capturing any 0+ chars other than linebreak symbols as few as possible (due to the lazy *?
, to account for the trailing "
later) but only if not equal to L
(1 or zero occurrences) or whitespaces followed with the end of string ( $
) and \\s*"?\\s*
will also account for the optional trailing "
((?!L?\\s*"?\\s*$).*?)
-组1捕获除换行符以外的任何0+字符,并且尽可能少(由于懒惰的*?
以解决尾随的"
以后),但只有当不等于L
(1或零次)或空格,接着与字符串的结尾( $
)和\\s*"?\\s*
也将占到可选尾随"
"?
- an optional "
"?
-可选的"
$
- end of string. $
-字符串结尾。 So, the (?!L?\\s*$)
negative lookahead will fail the match if "testing " is followed with: 因此,如果
(?!L?\\s*$)
后面跟有(?!L?\\s*$)
否定超前查询,将使匹配失败:
L
L
and whitespaces... L
和空格... And optional "
. 和可选
"
。
var ss = [ '"testing rest"', '"testing what"', '"testing Loong"', '"testing N"', '"testing L"', '"testing"', '"testing "' ]; // Test strings var rx = /^"?testing\\s+((?!L?\\s*"?\\s*$).*?)"?$/; for (var s = 0; s < ss.length; s++) { // Demo document.body.innerHTML += "Testing \\"<i>" + ss[s] + "</i>\\"... "; document.body.innerHTML += "Matched: <b>" + ((m = ss[s].match(rx)) ? m[1] : "NONE") + "</b><br/>"; }
And if you just want to avoid matching a "testing " string with L
at the very end (before optional "
) you may shorten the pattern to 而且,如果您只是想避免在最后将“测试”字符串与
L
匹配(在可选的"
之前"
),则可以将模式缩短为
/^"?testing\s((?!L?"?$).*?)"?$/
See this regex demo (the \\s
is replaced with a space in the demo since the test is performed against a multiline string) 请参阅此正则表达式演示 ( 演示中的
\\s
被空格替代,因为测试是针对多行字符串执行的)
This is the regex that you want. 这是您想要的正则表达式。 It matches string starting with testing then one or more space characters then word characters that are atleaset 2 or more in size.
它匹配从测试开始的字符串,然后是一个或多个空格字符,然后是至少2个或更多大小的单词字符。
/^testing\s+\w{2,}/
我相信这是您要查找的正则表达式:
/^"(testing(?: )?.*)"$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.