Solved

Javacsript replace();

Posted on 2015-02-06
5
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 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 83

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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

630 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