1

I can't make this regex work in javascript:

pat = new RegExp("Lorem(?=([^<]*($|<a|<[^/]|</[^a]))*($|(?<=a)))", "g");
test = test.replace(pat, '<a href="#">Lorem</a>');

I get the error: invalid quantifier.

But here is working: http://gskinner.com/RegExr/?2va0g

Thanks for your help :)

EDIT

I need to replace every occurrence of "String" with a link, but only if "String" wasn't already a link.

Es.

This is a <a href="#">String</a>. This is another String.

=>

This is a <a href="#">String</a>. This is another <a href="#">String</a>.

Better if can check also the url of the link es:

This is a <a href="http://www.test.com/String">link</a>

=>

This is a <a href="http://www.test.com/String">link</a>

Probably is enough to replace all String that are not inside <a ... </a> (the first a TAG is not closed on purpose)

  • 4
    `?<=` lookbehinds are not supported in JS. – Fabrício Matté Oct 21 '12 at 21:20
  • That's a pity, well thanks for your help, i'll have to find another solution. – user1763784 Oct 21 '12 at 21:23
  • 1
    Well, if you update the question to explain what's the exact expected behavior of your regex, someone may be able to help adapting it. – Fabrício Matté Oct 21 '12 at 21:23
  • 2
    This is JavaScript, *the* language for dom manipulations. And yet you're STILL use regex to parse HTML. WHY?! – Madara's Ghost Oct 21 '12 at 21:45
  • Because the text that I want to replace is in a textarea, precisely is the content of a tinyMCE that I get with: parent.tinyMCE.activeEditor.getContent() – user1763784 Oct 21 '12 at 21:51
  • [Don't use Regex to parse HTML](http://stackoverflow.com/a/1732454/1048572). This is also true for manipulation. As you seem to be in a browser environment, it really shouldn't be hard to use the DOM – Bergi Oct 21 '12 at 21:58
  • So then parse it, getting back DOM nodes that you can inspect and operate on. Even if you never append them to the page, you can still treat them like DOM nodes. – Alex Wayne Oct 21 '12 at 21:58
  • @user1763784: So? http://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro – Madara's Ghost Oct 21 '12 at 22:00
  • @MadaraUchiha - In defense of the OP (note, I'm not a regex fan, that high-brow gobbledygook), you really haven't shown *how* to accomplish the same effect with DOM methods either. All you've done is point out it's an option, but how would that look? – Jared Farrish Oct 21 '12 at 22:04
  • Well, I can operate on them as DOM nodes, but at the end of the day I still have to replace the String with the link :/ I can't see the advantage of that method, maybe an example could help. – user1763784 Oct 21 '12 at 22:06
  • Welcome to SO, btw. The back-and-forth is normal. Don't be discouraged. – Jared Farrish Oct 21 '12 at 22:07
  • @user1763784: Replacing a string for the link isn't the problem. The problem is finding the string, making sure it's not a tag name, or an attribute name, or an attribute value. Or part of a link, or one of the other million causes why you may not want it to occur. – Madara's Ghost Oct 21 '12 at 22:19
  • I've solved using this: http://james.padolsey.com/javascript/find-and-replace-text-with-javascript/ – user1763784 Oct 22 '12 at 01:37

1 Answers1

0

See this example Here

var toreplace = "String"; // Text to find
var rx = new RegExp ("((?!\/)" + toreplace + "(?!\"))(?!<(.*?)>)", "g");
var txt = "This is a <a href=\"#\">String</a>. This is another String."; // Text to replace.
var newtxt = txt.replace (rx, '<a href="#">$1</a>'); // New Text

The result is:

This is a <a href="#">String</a>. This is another String.

=>

This is a <a href="#">String</a>. This is another <a href="#">String</a>.
atiruz
  • 2,782
  • 27
  • 36