简体   繁体   中英

How to match all words starting with dollar sign but not slash dollar

I want to match all words which are starting with dollar sign but not slash and dollar sign. I already try few regex.

(?:(?!\\)\$\w+)

\\(\\?\$\w+)\b

String

$10<i class="">$i01d</i>\$id

Expected result

*$10*

*$i01d*

but not this

*$id*

After find all expected matching word i want to replace this my object.

One option is to eliminate escape sequences first, and then match the cleaned-up string:

 s = String.raw`$10<i class="">$i01d</i>\\$id` found = s.replace(/\\\\./g, '').match(/\\$\\w+/g) console.log(found) 

The big problem here is that you need a negative lookbehind , however, JavaScript does not support it. It's possible to emulate it crudely, but I will offer an alternative which, while not great, will work:

 var input = '$10<i class="">$i01d</i>\\\\$id'; var regex = /\\b\\w+\\b\\$(?!\\\\)/g; //sample implementation of a string reversal function. There are better implementations out there function reverseString(string) { return string.split("").reverse().join(""); } var reverseInput = reverseString(input); var matches = reverseInput .match(regex) .map(reverseString); console.log(matches); 

It is not elegant but it will do the job. Here is how it works:

JavaScript does support a lookahead expression ( (?>) ) and a negative lookahead ( (?!) ). Since this is the reverse of of a negative lookbehind, you can reverse the string and reverse the regex, which will match exactly what you want. Since all the matches are going to be in reverse, you need to also reverse them back to the original.

It is not elegant, as I said, since it does a lot of string manipulations but it does produce exactly what you want.

See this in action on Regex101

Regex explanation Normally, the "match x long as it's not preceded by y " will be expressed as (?<!y)x , so in your case, the regex will be

/(?<!\\)\$\b\w+\b/g

demonstration (not JavaScript)

where

(?<!\\) //do not match a preceding "\"
  \$ //match literal "$"
  \b //word boundary
  \w+ //one or more word characters
  \b //second word boundary, hence making the match a word

When the input is reversed, so do all the tokens in order to match. Furthermore, the negative lookbehind gets inverted into a negative lookahead of the form x(?!y) so the new regular expression is

/\b\w+\b\$(?!\\)/g;

This is more difficult than it appears at first blush. How like Regular Expressions!

If you have look-behind available, you can try:

/(?<!\\)\$\w+/g

This is NOT available in JS. Alternatively, you could specify a boundary that you know exists and use a capture group like:

/\s(\$\w+)/g

Unfortunately, you cannot rely on word boundaries via /b because there's no such boundary before '\\'.

Also, this is a cool site for testing your regex expressions. And this explains the word boundary anchor.

If you're using a language that supports negative lookback assertions you can use something like this .

(?<!\\)\$\w+

I think this is the cleanest approach, but unfortunately it's not supported by all languages.

This is a hackier implementation that may work as well.

(?:(^\$\w+)|[^\\](\$\w+))

This matches either

  1. A literal $ at the beginning of a line followed by multiple word characters. Or...

  2. A literal $ this is preceded by any character except a backslash.

Here is a working example .

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