Possible Duplicate:
How to replace last occurrence of characters in a string using javascript
This is my code :
var newValue= myString.replace(" ", "<br />");
but I’d like to replace only the last occurance, not ALL occurances.
Such as :
Hello my name is Marco
become
Hello my name is<br />Marco
How can I do it? I think regex?
If you want to go with regex, I can only think of
replace(/ (?=[^ ]*$)/, "<br />"). Not the best option, and does not work for more complicated regexes because it requires a “negation” of the whole expression.Maybe it’s easier with simple string manipulation:
You also could use backward search: