Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex converting & to &

I am developing a small character encoder generator where the user input their text and on the click of a button, it outputs the encoded version.

I've defined an object of the characters that need to be encoded like so:

map = {
    '©' : '©',
    '&' : '&'
},

And here is the loop that gets the values from the map and replaces them:

Object.keys(map).forEach(function (ico) {
    var icoE   = ico.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
    raw = raw.replace( new RegExp(icoE, 'g'), map[ico] );
});

I am them simply outputting the result to a textarea. This all works fine, however the problem I'm facing is this.

© is replaced with © however the & symbol at the beginning of this is then converted to & so it ends up being ©.

I see why this is happening however I'm not sure how to go about ensuring that & is not replaced within character encoded strings.

Here is a JSFiddle for a live preview of what I mean:

http://jsfiddle.net/4m3nw/1/

Any help would be much appreciated

like image 598
Lodder Avatar asked Mar 20 '26 04:03

Lodder


1 Answers

Prelude: Apart from regex, an idea worth considering is something like this JS function that already handles html entities. Now, on to the regex question.

HTML Special Characters, Negative Lookahead

In HTML, special characters can look not only like © but also like —, and they can have upper-case characters.

To replace ampersands that are not immediately followed by a hash or word characters and a semicolon, you can use something like this:

&(?!(?:#[0-9]+|[a-z]+);)

See the demo.

  • Make sure to use the i flag to activate case-insensitive mode
  • & matches the literal ampersand
  • The negative lookahead (?!(?:#[0-9]+|[a-z]+);) asserts that it is not followed by...
  • (?:#[0-9]+|[a-z]+) a hash and digits, | OR letters...
  • then a semicolon.

Reference

  • Lookahead and Lookbehind Zero-Length Assertions
  • Mastering Lookahead and Lookbehind
like image 141
zx81 Avatar answered Mar 22 '26 18:03

zx81



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!