Solved

Javacsript replace();

Posted on 2015-02-06
5
83 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 82

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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
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 82

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now