javascript - Regex converting & to & -
javascript - Regex converting & to & -
i developing little character encoder generator user input text , on click of button, outputs encoded version.
i've defined object of characters need encoded so:
map = { '©' : '©', '&' : '&' },
and here loop gets values map , replaces them:
object.keys(map).foreach(function (ico) { var icoe = ico.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); raw = raw.replace( new regexp(icoe, 'g'), map[ico] ); });
i them outputting result textarea. works fine, problem i'm facing this.
©
replaced ©
&
symbol @ origin of converted &
ends beingness ©
.
i see why happening i'm not sure how go ensuring &
not replaced within character encoded strings.
here jsfiddle live preview of mean:
http://jsfiddle.net/4m3nw/1/
any help much appreciated
prelude: apart regex, thought worth considering this js function handles html entities. now, on regex question.
html special characters, negative lookahead
in html, special characters can not ©
—
, , can have upper-case characters.
to replace ampersands not followed hash or word characters , semicolon, can utilize this:
&(?!(?:#[0-9]+|[a-z]+);)
see demo.
make sure utilizei
flag activate case-insensitive mode &
matches literal ampersand the negative lookahead (?!(?:#[0-9]+|[a-z]+);)
asserts not followed by... (?:#[0-9]+|[a-z]+)
hash , digits, |
or letters... then semicolon. reference
lookahead , lookbehind zero-length assertions mastering lookahead , lookbehind javascript regex replace
Comments
Post a Comment