/    Sign up×
Community /Pin to ProfileBookmark

Javascript Replace string problem

before
var str = “the The the The Lord of the Rings: The Fellowship of the Ring”;
var result = str.replace(/The/gi, “Ze”);

after
var str = M1.value;

————————–(Textbox1)——(Textbox2)
var result = str.replace(/ +’M1.value’+ /gi, “+M2.value+”);

I can’t get my M1.value and M2.value textbox’s to work in the replace str.

all I know is I am leaving out ‘ ” ‘ ‘ parentheses or something like that?

Thanks for any help ben

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Oct 21.2008 — Too many actuallyvar result = str.replace(new RegExp(M1.value,"gi",M2.value);You have to use new RegExp("regular expression") when you want to pass a string.
Copy linkTweet thisAlerts:
@orlandoauthorOct 21.2008 — I get an error "error on page".
[CODE]
<script type="text/javascript">

function del45a2(){


var result = str.replace(new RegExp(M1.value,"gi",M2.value);

document.write(result);
}
</script>
[/CODE]


<!-- Htlm body-->

<button onclick="del45a2();">Start</button>
Copy linkTweet thisAlerts:
@DokOct 21.2008 — Declan missed a parenthesis
[CODE]var result = str.replace(new RegExp(M1.value,"gi"[COLOR="Red"])[/COLOR],M2.value);[/CODE]
Copy linkTweet thisAlerts:
@orlandoauthorOct 22.2008 — [CODE]
alert("1") <!-- this alert shows -->

var result = str.replace(new RegExp(M1.value, "gi"),M2.value); <!--whats wrong -->

alert("2") <!-- this alert does not -->

[/CODE]

?
Copy linkTweet thisAlerts:
@orlandoauthorOct 22.2008 — Ok...got it to work
<i>
</i>&lt;html&gt;&lt;head&gt;




&lt;script language="JavaScript"&gt;
function del45a3(){
var string = M0.value;
string = string.replace(new RegExp(M1.value, "gi"),M2.value);
M3.value=string;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type='text' id='M0' value='abbbabbbbbbaccccccakkkkk' /&gt;replace all the a's with .101. &lt;BR&gt;
&lt;input type='text' id='M1' value='a' /&gt;M1&lt;BR&gt;
&lt;input type='text' id='M2' value='.100.' /&gt;M2&lt;BR&gt;
&lt;input type='text' id='M3' value='' /&gt;M3&lt;BR&gt;



&lt;button onclick="del45a3();"&gt;Start&lt;/button&gt;
&lt;/body&gt;&lt;/html&gt;

Good Day!
×

Success!

Help @orlando spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...