Substituting with regular expressions, in place

S

Sally B.

Hi, how would I go about doing the following: substitute all the
strings 'the' and 'dog' in the 'mytext' id div only? The following
doesn't work for 'the'. I also need to use regular expressions for the
replace command... but I can't get that far. Any reason this doesn't
work as below:

<script>
function start() {
theText=document.getElementById('mytext');
theText.replace(/the/g, "xxxx");
}
</script>
</head>
<body onload="start()">
<p>the quick brown fox jumps over the lazy dog</p>
<div id=mytext>the quick brown fox jumps over the lazy dog</div>
</body>
</html>


I have researched a bit and all the 'replace' examples I think should
work, don't. Also, is this even the best way to do it? If I have a big
chunk of text in a <div>, what would be a better way to search and
replace with regular expressions?

Thanks!
Sally-B
 
T

Tomasz Cenian

Sally B. napisał(a):
<script>
function start() {
theText=document.getElementById('mytext');

"theText" here refers to the div element, not to its contents, so you
would need sth like:

el=document.getElementById('mytext');
theText=el.innerHTML;

theText.replace(/the/g, "xxxx");

you do not assign the return value. It gets lost. You would need:

el.innerHTML=theText.replace(/the/g, "xxxx");

}
</script>

<div id=mytext>the quick brown fox jumps over the lazy dog</div>

<div id="mytext">the quick brown fox jumps over the lazy dog</div>

Every attribute must have its value quoted
 
S

Sally B.

ahh... thanks. that works. Now, how do I define an array of regular
expressions and corresponding replacement strings? In php, you could do
something like:
$was = {/the/, /dog/};
$new = {"xxxxx", "cat"};

etc, then use a preg_replace($was, $new, $string) to replace the
strings...

How would I do that in JavaScript?

Again, is this even the best way to do what I am trying to do: take a
large block of text and do regex replacements on it?

Thanks!
Sally
 
M

Mick White

Sally said:
ahh... thanks. that works. Now, how do I define an array of regular
expressions and corresponding replacement strings? In php, you could do
something like:
$was = {/the/, /dog/};
$new = {"xxxxx", "cat"};

etc, then use a preg_replace($was, $new, $string) to replace the
strings...

How would I do that in JavaScript?

String.replace(regex,replacement)

"The ink".replace(/[e ]/,"")

Mick
 
S

Sally B.

Mick said:
Sally said:
ahh... thanks. that works. Now, how do I define an array of regular
expressions and corresponding replacement strings? In php, you could do
something like:
$was = {/the/, /dog/};
$new = {"xxxxx", "cat"};

etc, then use a preg_replace($was, $new, $string) to replace the
strings...

How would I do that in JavaScript?

String.replace(regex,replacement)

"The ink".replace(/[e ]/,"")

Mick

Is there a way to have, say, 5 different regexs and 5 different
(corresponding) replacement strings without having 5 separate 'replace'
commands?

Thanks
 
L

Lee

Sally B. said:
Mick said:
Sally said:
ahh... thanks. that works. Now, how do I define an array of regular
expressions and corresponding replacement strings? In php, you could do
something like:
$was = {/the/, /dog/};
$new = {"xxxxx", "cat"};

etc, then use a preg_replace($was, $new, $string) to replace the
strings...

How would I do that in JavaScript?

String.replace(regex,replacement)

"The ink".replace(/[e ]/,"")

Mick

Is there a way to have, say, 5 different regexs and 5 different
(corresponding) replacement strings without having 5 separate 'replace'
commands?

You can put your patterns and replacement texts in arrays and loop through the
pairs:

pat = [ /the/, /dog/ ];
repl = [ "xxxxx", "cat" ];

for (var i=0;i<pat.length;i++) {
myString=myString.replace(pat,repl);
}

Note that /the/ also matches "other", etc.
 
S

Sally B.

Thanks... also, how do I grab the entire body of the document instead
of just an Id tag. I tried
"theText=document.getElementByTag('body');"
to grab the entire body, but no go. Is this because the 'onload' call
is in the body tag?

Thanks!
 
M

Mick White

Sally said:
Thanks... also, how do I grab the entire body of the document instead
of just an Id tag. I tried
"theText=document.getElementByTag('body');"
to grab the entire body, but no go. Is this because the 'onload' call
is in the body tag?

theText=document.body.innerHTML;

Not quite Kosher, but it should do what you want.

Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top