开发者

Regular Express issue - javascript

开发者 https://www.devze.com 2023-03-12 08:49 出处:网络
I am trying to replace a text in javascript using a regular expression like following [example of what I am trying to do]:

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 开发者_C百科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 :)


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>


Make your replace:

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

Which basically means "Old not in an HTML tag"


Wouldn't it be simpler to assign id to <a> element instead and then run the same replace() on it's innerHTML (which shouldn't contain the tag's title attribute):

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号