Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that this question isn't about parsing. This is lexing. Something that regex are regularly and properly used for.</p> <p>If you want to go with regex there are a couple of ways you could do this.</p> <ul> <li><p>A simple hack lookahead like:</p> <pre><code>a(?![^&lt;&gt;]*&gt;) </code></pre> <p>note that this wont handle <code>&lt;</code> and <code>&gt;</code> quoted in tags/unescaped outside of tags properly.</p></li> <li><p>A full blown tokenizer of the form:</p> <pre><code>(expression for tag|comments|etc)|(stuff outside that that i'm interested in) </code></pre> <p>Replaced with a function that does different things depending on which part was matched. If <code>$1</code> matched it would be replaced by it self, if <code>$2</code> matchehd replace it with <code>*$2*</code></p></li> </ul> <p><br> The full tokenizer way is of course not a trivial task, the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html" rel="nofollow noreferrer">spec isn't small</a>.</p> <p>But if simplifying to only match the basic tags, ignore CDATA, comments, script/style tags, etc, you could use the following:</p> <pre><code>var str = '&lt;span class="a &lt;lal&gt; a" attr&gt;habbitant 2 &gt; morbi. 2a &lt; 3a&lt;/span&gt; triastbbitique'; var re = /(&lt;[a-z\/](?:"[^"]*"|'[^']*'|[^'"&gt;]+)*&gt;)|(a)/gi; var res = str.replace(re, function(m, tag, a){ return tag ? tag : "*" + a + "*"; }); </code></pre> <p>Result:</p> <pre><code>&lt;span class="a &lt;lal&gt; a" attr&gt;h*a*bbit*a*nt 2 &gt; morbi. 2*a* &lt; 3*a*&lt;/span&gt; tri*a*stbbitique </code></pre> <p>This handles messy tags, quotes and unescaped <code>&lt;</code>/<code>&gt;</code> in the HTML.</p> <p><br> Couple examples of tokenizing HTML tags with regex (which should translate fine to JS regex):</p> <ul> <li><a href="https://stackoverflow.com/questions/9462104/remove-on-js-event-attributes-from-html-tags/9466152#9466152">Remove on* JS event attributes from HTML tags</a></li> <li><a href="https://stackoverflow.com/questions/9494143/regex-to-allow-only-set-of-html-tags-and-attributes/9499761#9499761">Regex to allow only set of HTML Tags and Attributes</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload