Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Express issue - javascript

I am trying to replace a text in javascript using a regular expression like following [example of what I am trying to do]:

<html><head></head>
<body>
  <div id="div1"><a href="#" title="This is Old">This is Old</a></div>
  <script> 
    var message = "Hi";
    var str = document.getElementById("div1").innerHTML;
    var newstr = str.replace(/Old/g, "<div onclick='say(\""+message+"\");'>New</div>");
    document.getElementById("div1").innerHTML = newstr;
    function say(message)
    {
      alert(message);
    }
  </script>
</body>
</html>

This is a sample code of what I am trying to do in a large application. Our script is injected in thrid party html pages, so we dont have control over the html. If you run this code, you will see that the text appears to be broken and if you just remove the "Old" from the title tag it will work fine. I cannot change the html, so I have to modify the script to handle this.

Is there a way I can put some regular express that can bypass the replacement of the text in case if it occurs in between "<" and ">"? or some other way to solve this.

I cannot use DOM to do the replacement, as it crashed the page when there were too much text, I am doing full page text replacement.

Thanks in advance for your help :)

EDIT: Changed the code to make it working :)

like image 607
happyhardik Avatar asked Dec 21 '25 22:12

happyhardik


2 Answers

I might be unrelated but, you may need to replace message variable inside the replaced text. Since you declared message variable locally, it will not be available outside.

EDIT:
For your question, you can do that with RegEx but it will be quite hard. If I got time I might work on it a bit.

EDIT 2:
Try this one, it makes sure the Old is not in a tag.

>[^><]*(Old)[^<>]*<

EDIT 3:
This works file too, starting > is not necassary

[^><]*(Old)[^<>]*<

EDIT 4:

<html><head></head>
<body>
  <div id="div1"><a href="#" title="This is Old">This is Old</a></div>
  <script> 
    var message = "Hi";
    var str = document.getElementById("div1").innerHTML;
    var newstr = str.replace(/([^><]*)Old([^<>]*<)/g, "$1<div onclick='say(\""+message+"\");'>New</div>$2");
    document.getElementById("div1").innerHTML = newstr;
    function say(message)
    {
      alert(message);
    }
  </script>
</body>
</html>
like image 189
Cem Kalyoncu Avatar answered Dec 23 '25 10:12

Cem Kalyoncu


Make your replace:

str.replace(/(>[^<]*)Old/g, "$1<div onclick='say(\"message\");'>New</div>");

Which basically means "Old not in an HTML tag"