简体   繁体   中英

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"

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.

Then, there is atomic group (in round brackets): [^L]|..+ which consists of OR statement (a pipe).

On the left side of this OR we have a search pattern for all one character strings (except for letter " 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).

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: + ).

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:

/^"?testing\s+((?!L?\s*"?\s*$).*?)"?$/

See the regex demo

Details :

  • ^ - start of string
  • "? - an optional "
  • testing - a literal string testing
  • \\s+ - 1 or more whitespaces
  • ((?!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 "
  • "? - an optional "
  • $ - end of string.

So, the (?!L?\\s*$) negative lookahead will fail the match if "testing " is followed with:

  • end of string
  • L
  • whitespaces
  • L and whitespaces...

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

/^"?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)

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.

/^testing\s+\w{2,}/

我相信是您要查找的正则表达式:

/^"(testing(?: )?.*)"$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM