Home > Mobile >  Regex - Match escape characters inside attributes
Regex - Match escape characters inside attributes

Time:01-24

Well, I'm not an expert with regex.

My problem is simple, Im trying to match some escape characters from a string which have this format (It is a string before I parse it to a DOM)

<info type="map" name="Double quotes test name" author="Escape < character"></info>
<info type="map" name='Test name with single quotes' author='Escape < character'></info>

As you can see, there are 2 types of properties that I'm trying to match, these are name and author. I want to convert < character to &lt, however, my patter is not matching properly.

My pattern currently match the whole attribute value. It even matches attributes which aren't author or name.

/(?!author|name\s*=\s*)(?:\'[^']*\')/g

I hope you can bring me a hand with this, thanks for reading and best regards.

CodePudding user response:

You can try matching all < that are not preceeded by either newline, start of string or >:

(?<=[^\n>])<

Check the demo here.


If you want to make sure the < is found within the value of either the name or author attribute, you can use:

(?<=(?:author|name)=(?:"[^<"] ?|'[^<'] ?))<

where the < is preceeded by:

  • (?:author|name): either "author" or "name" keyword
  • =: equals
  • (?:"[^<"] ?|'[^<'] ?):
    • "[^<"] ?: a " followed by least amount of characters which don't include " and <
    • |: or
    • '[^<'] ?: a ' followed by least amount of characters which don't include ' and <

Check the demo here.

  • Related