Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  5] [ 2]  / answers: 1 / hits: 20540  / 13 Years ago, tue, may 3, 2011, 12:00:00

Suppose I have string variable such as:



var a = xxxxxxxxhelloxxxxxxxx;


or:



var a = xxxxhelloxxxx;


I want to insert world after hello.



I can't use substr() because the position is not known ahead of time. How can I do this in JavaScript or jQuery?


More From » string

 Answers
13



var a = xxxxhelloxxxxhelloxxxx;
a = a.replace(/hello/g,hello world); // if you want all the hello's in the string to be replaced
document.getElementById(regex).textContent = a;

a = xxxxhelloxxxxhelloxxxx;
a = a.replace(hello,hello world); // if you want only the first occurrence of hello to be replaced
document.getElementById(string).textContent = a;

<p>With regex: <strong id=regex></strong></p>
<p>With string: <strong id=string></strong></p>




[#92439] Saturday, April 30, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeffn

Total Points: 559
Total Questions: 81
Total Answers: 103

Location: Spain
Member since Thu, Dec 23, 2021
3 Years ago
;