1

I know that this regexp replace: something.replace(/something/g,"something"); replaces every occurrence in a string, but how do I replace all of something with a jQuery/JavaScript expression because something like this :

newhtml=newhtml.replace(wrap($('<span class="charachter">' + prop + '</span>'))/g, wrap($('<span class="charachter">' + prop + '</span>'), obj[prop]));

doesn't work when it is turned into something like this:

newhtml=newhtml.replace(/wrap($('<span class="charachter">' + prop + '</span>'))/g, wrap($('<span class="charachter">' + prop + '</span>'), obj[prop]));

What am I suppose to do?

2
  • The .replace() function works with Strings. What are you trying to replace? What you have looks the same in both parts of the replace Commented Dec 2, 2011 at 2:44
  • I'm really not sure what it is you're trying to do there. Commented Dec 2, 2011 at 2:44

2 Answers 2

2

Regexes are for strings only. What is happening is that it is converting your text that would normally select the nodes to a string, and is trying to match those characters, which will be no matches, so it will do nothing.

If you want to replace a node, then you either need to modify the node properties or remove the node and add a new one in its same place.

Note, You could probably use the jQuery replaceWith method (and maybe with the clone method depending on what you want to do) to give the 1 node all of the node properties that you are trying to replace with.

$('#node1').replaceWith($('#node2'));  //Replace node1 with node2
$('#node1').replaceWith($('#node2').clone());  // Replace node1 with a copy of node2
Sign up to request clarification or add additional context in comments.

4 Comments

You may also want to use replaceWith() to replace the element with a new one.
I'm trying to replace a string with html in it, not a actual node. Hope you understand what I mean
So, you want to replace the HTML of the first node with the HTML of the second? If this is not the case, can you please add some clarification to your question?
Or just with HTML from something else? Like an object as it looks like you have with something like 'obj[prop]'
0

I realize the other poster's answer was probably more closely aligned with what you need, but if you want to do fancy regexes with variables in them, you need to use the RegExp constructor.

var text = "old stuff"
var stuff = "lots of old stuff"
var newStuff = stuff.replace(new RegExp(text, 'g'), "new stuff") // lots of new stuff

More docs on replace: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

As for you maybe you'd want to do something like this:

var $span = $("<span class='rarar'>bla bla bla</span>") // create the new element
var html = $span.text() // bla bla bla
var text = html.replace(new RegExp("bla", "g"), "go")
$span.text(text) // <span class='rarar'>go go go</span>
$span.appendTo(document.body) // add it to the body

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.