chopper draw lion4
chopper draw lion4

Reputation: 13487

Regex: How do I remove the character BEFORE the matched string?

I am intercepting messages which contain the following characters:

*_-

However, whenever any one of these characters comes through, it will always be preceded by a \. The \ is just for formatting though and I want to remove it before sending it off to my server. I know how to easily create a regex which would remove this backslash from a single letter:

'omg\_bbq\_everywhere'.replace(/\\_/g, '')

And I recognize I could just do this operation 3 times: once for each character I want to remove the preceding backslash for. But how can I create a single regex which would detect all three characters and remove the preceding backslash in all 3 cases?

Upvotes: 5

Views: 2654

Answers (2)

Michael P Metully
Michael P Metully

Reputation: 11

This is a good time to use atomic matching. Specifically you want to check for the slash and then positive lookahead for any of those characters.

Ignoring the code, the raw regex you want is:

\\(?=[*_-])

A literal backslash, with one of these characters in front of it: *_-

So now you are matching the slash. The atomic match is a 0 length match, so it doesn't match anything, but sets a requirement that "for this to be a valid match, it needs to be followed by [*_-]"

Atomic groups: http://www.regular-expressions.info/atomic.html

Lookaround statements: http://www.regular-expressions.info/lookaround.html

Positive and negative lookahead and lookbehind matches are available.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You can use a character class like [*_-].

To remove only the backslash before these characters:

document.body.innerHTML =
   "omg\\-bbq\\*everywhere\\-".replace(/\\([*_-])/g, '$1');

When you place a subpattern into a capturing group ((...)), you capture that subtext into a numbered buffer, and then you can reference it with a $1 backreference (1 because there is only one (...) in the pattern.)

Upvotes: 3

Related Questions