JRS: In article <y7**********@hotpop.com>, dated Tue, 25 Apr 2006
23:11:15 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com> posted :
"CK" <c_**********@hotmail.com> writes:
Theis should be easy. I need to insert a string into an exisitng string?
Example
var string1 = "sweet";
var string2 = "bbb";
I want to modify string1 to be "swebbbet";
string1 = "swebbbet";
But seriously, I assume you have some position where you want it.
There must be some definition (even if it is "at random"), but the
condition may be given other than as a number.
Then you can build the result by concatenating substrings:
string1 = string1.substring(0,3) + string2 + string1.substring(3);
Or by
string1 = string1.replace(/(.{3})/, "$1" + string2) // position 3
string1 = string1.replace(/(e)/, "$1" + string2) // after first e
string1 = string1.replace(/(e[^e]+)/, string2 + "$1") // before ...
// ... the first e followed by a non-e
string1 = string1.replace(/(..)$/, string2 + "$1") // two from end
string1 = string1.replace(/ee/g, "e" + string2 + "e") // in every 'ee'
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.