简体   繁体   English

JavaScript正则表达式匹配除字母以外的所有内容

[英]JavaScript regex match anything except a letter

I need to match the specific string that comes after "testing" 我需要匹配“测试”之后的特定字符串

  • provided that there is one (so avoiding matching "testing" alone) 只要有一个(因此避免单独匹配“测试”)
  • and avoiding the match if that string is specifically the letter "L" 如果该字符串是字母“ L”,则避免匹配

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*$)否定超前查询,将使匹配失败:

  • end of string 字符串结尾
  • L
  • whitespaces 空格
  • 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.

相关问题 正则表达式匹配大括号之间的任何内容,但引用时除外 - Regex to match anything between braces except when quoted javascript正则表达式不匹配特定属性内的任何内容 - javascript regex match anything not inside a specific attribute 正则表达式使用JavaScript用增量字母替换数字以外的任何内容 - Regex replace anything but numbers with increments letter using javascript JavaScript正则表达式用第一个字母替换单词,但在括号内除外 - JavaScript Regex replace words with their first letter except when within parentheses 除了给定字符串之外,匹配任何内容的JavaScript模式是什么? - What would be the JavaScript pattern to match anything except a given string? JavaScript 正则表达式 - 在空格后的字母之间添加 ~ 符号 - JavaScript Regex - add ~ symbol between lettes except letter after space 正则表达式 javascript 通过第一个字母匹配第二个字母,但第二个字母必须是大写 - Regex javascript match the second letter by the first letter, but the second letter must be an uppercase 试图在javascript中使用某些字母后跟任意数字的正则表达式匹配 - trying to get a regex match in javascript with certain letter followed by any number 正则表达式将字母+数字匹配为单独的单词(javascript) - regex to match letter+number as a seperate word (javascript) Javascript Regex-在单词中间(在句子中)匹配大写/大写字母吗? - Javascript Regex - match capital/upperacase letter in a middle of a word (in a sentence)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM