Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javacsript replace();

Posted on 2015-02-06
5
Medium Priority
?
101 Views
Last Modified: 2015-02-06
Does Javascript replace() only replace the FIRST instance of a substring found in a string?

For example, I have xyz = xyz.replace(""", '"');

It replaces the first instance, not the 2nd.

Specifically, I have a case where xyz = "A. "TRACE""

It only replaces the 1st (from the left) instance.

Do I have to loop through until they are "all gone"?

Thanks
0
Comment
Question by:Richard Korts
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 40594991
This page http://www.w3schools.com/jsref/jsref_replace.asp says you shouldn't have to.  I don't know if hex codes like " require any special consideration.

This little demo says that at least for " in Firefox, you have to loop thru it.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS Replace</title>
</head>
<body>
<h1>JS Replace</h1>
For example, I have xyz = xyz.replace("&#34;", "'");

It replaces the first instance, not the 2nd.

Specifically, I have a case where xyz = "A. &#34;TRACE&#34;"
<script type="text/javascript">
<!--
document.write("<br>");
var xyz = "A. &#34;TRACE&#34;";
document.write(xyz+"<br>");
xyz = xyz.replace("&#34;", "'");
document.write(xyz+"<br>");
xyz = xyz.replace("&#34;", "'");
document.write(xyz+"<br>");

// -->
</script>
<br />
Different string.
<script type="text/javascript">
<!--
var xyz = "A. 34TRACE34";
document.write(xyz+"<br>");
xyz = xyz.replace("34", "'");
document.write(xyz+"<br>");
xyz = xyz.replace("34", "'");
document.write(xyz+"<br>");

// -->
</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:Richard Korts
ID: 40595004
Ok, Dave.

I only expect two max. I'll set it for that & see what happens.

In most places I'm using php str_replace & it works perfectly, no matter how many.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40595104
Just to clarify here, the replace function expects either a string or a regular expression.  If you use the regular expression you can specify to replace the search term multiple times, i.e. using the global indicator

e.g. (taken from http://www.w3schools.com/jsref/jsref_replace.asp)

Example

Perform a global replacement:
var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/g, "red");

Open in new window

The result of res will be:
Mr Blue has a red house and a red car
0
 
LVL 43

Expert Comment

by:Rob
ID: 40595111
And if you have a dynamic variable that you need to search on (based on the data from http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_constructor):

var search_string = " ";  // replace all spaces
    var replace_string = "|";
    var patt = new RegExp(search_string, "g");
    var res = "Click the button to return the function that created the RegExp object's prototype.".replace(patt, replace_string);

Open in new window


res will contain Click|the|button|to|return|the|function|that|created|the|RegExp|object's|prototype.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40595134
Thanks for explaining that.  I saw it but it didn't register.  So here is the 'regex' version of the code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS Replace</title>
</head>
<body>
<h1>JS Replace</h1>
For example, I have xyz = xyz.replace(/&#34;/g, "'");

It replaces the first instance, not the 2nd.

Specifically, I have a case where xyz = "A. &#34;TRACE&#34;"
<script type="text/javascript">
<!--
document.write("<br>");
var xyz = "A. &#34;TRACE&#34;";
document.write(xyz+"<br>");
xyz = xyz.replace(/&#34;/g, "'");
document.write(xyz+"<br>");
xyz = xyz.replace(/&#34;/g, "'");
document.write(xyz+"<br>");

// -->
</script>
<br />
Different string.
<script type="text/javascript">
<!--
var xyz = "A. 34TRACE34";
document.write(xyz+"<br>");
xyz = xyz.replace(/34/g, "'");
document.write(xyz+"<br>");
xyz = xyz.replace(/34/g, "'");
document.write(xyz+"<br>");

// -->
</script>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question