简体   繁体   English

正则表达式:如何在匹配的字符串之前删除字符?

[英]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. 而且我知道我可以执行此操作3次:每个字符一次,我想删除前面的反斜杠。 But how can I create a single regex which would detect all three characters and remove the preceding backslash in all 3 cases? 但是,我怎样才能创建一个单一的正则表达式来检测所有三个字符并在所有3个案例中删除前面的反斜杠?

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.) 将子模式放入捕获组( (...) )时,将该子文本捕获到编号缓冲区中,然后可以使用$1反向引用来引用它(1因为只有一个(...)图案。)

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 [*_-]" 原子匹配是一个0​​长度匹配,所以它不匹配任何东西,但设置一个要求“为了这是一个有效的匹配,它需要后跟[* _-]”

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

Lookaround statements: http://www.regular-expressions.info/lookaround.html 外观陈述: http//www.regular-expressions.info/lookaround.html

Positive and negative lookahead and lookbehind matches are available. 可以使用正面和负面的前瞻和后仰比赛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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