javascript - Matching special characters with Regex lookahead -
i receiving messages have underscores:
_omgitworks_ however, when these messages formatted our html wire formatter receive them backslashes infront of underscores:
\_omgitworks\_ i created following regex capture backslashes , remove them text tokenizes correctly:
rawinput.replace(/\\([_])/g, '$1'); however, there edge case of when user italicizes text. when user utalicizes text, receive messages no backslashes underscores - , want remove underscores. received text looks like:
_omgitworks_ i trying design regex matches backslash followed underscore , replaces backslash (but not underscore) or if there underscore no bachslash, matches underscore.
i tried implement using lookaheads:
var regex = /\\(?=_)|_/g; var string = '\\_omgitworks\\_' string.replace(regex, '') >>> "omgitworks" but removing backslash , underscore instead of underscore. there nuances lookaheads overlooking?
you can use following regex:
\\(_)|(^|[^\\])_ and replace $1$2.
see regex demo
explanation:
\\(_)- first alternative matching\followed_(capture group 1)|- or...(^|[^\\])_- alternative matching start of string or character other\(captured group 2) followed underscore.
in relacement part, restore captures using backreferences. in js, failed groups pre-populated empty string, safe use if capture groups happen empty.
var re = /\\(_)|(^|[^\\])_/g; var str = '_omgitworks_\n\\_omgitworks\\_'; var result = str.replace(re, '$1$2'); document.body.innerhtml = "<pre>"+ result + "</pre>";
Comments
Post a Comment