javascript - Regex: How do I remove the character BEFORE the matched string? -
i intercepting messages contain following characters:
*_-
however, whenever 1 of these characters comes through, preceded \
. \
formatting though , want remove before sending off server. know how create regex remove backslash single letter:
'omg\_bbq\_everywhere'.replace(/\\_/g, '')
and recognize operation 3 times: once each character want remove preceding backslash for. how can create single regex detect 3 characters , remove preceding backslash in 3 cases?
you can use character class [*_-]
.
to remove backslash before these characters:
document.body.innerhtml = "omg\\-bbq\\*everywhere\\-".replace(/\\([*_-])/g, '$1');
when place subpattern capturing group ((...)
), capture subtext numbered buffer, , can reference $1
backreference (1 because there 1 (...)
in pattern.)
Comments
Post a Comment