Solved

Replace last segment of string

Posted on 2009-07-02
4
201 Views
Last Modified: 2012-05-07
I need a function that will take in an argument and replace the last segment of a string with that argument. Separator is a colon.

example1:
pass in "newSegName"
string is - "seg1:seg2:seg3:seg4"
returns - "seg1:seg2:seg3:newSegName"

example2:
pass in "newSegName2"
string is - "seg1:seg2:seg3:seg4:seg5:seg6"
returns - "seg1:seg2:seg3:seg4:seg5:newSegName"
0
Comment
Question by:894359
  • 2
4 Comments
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 200 total points
Comment Utility
Code snippet attached
<script>

var str = "seg1:seg2:seg3:seg4";

var r_str = "newSegName";

var str_arr = str.split(":");

str_arr[str_arr.length-1] = r_str;

alert(str_arr.join(":"));

</script>

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
Comment Utility
in another option by regular expression
<script type="text/javascript">
var str = "seg1:seg2:seg3:seg4";
var r_str = "newSegName";
var replaceRegExp = new RegExp (":[^:]+$", "g");
var newString = str.replace(replaceRegExp, ":"+r_str);
document.write(newString);
</script>
0
 
LVL 15

Expert Comment

by:fsze88
Comment Utility
or this
<script type="text/javascript">
var str = "seg1:seg2:seg3:seg4";
var r_str = "newSegName";
var replaceRegExp = new RegExp ("^(.+:)([^:]+)$", "g");
var newString = str.replace(replaceRegExp, "$1" + r_str);
document.write(newString);
</script>
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 300 total points
Comment Utility
try this

<script>
function newString(add,str){
   return str.replace(/:[^:]*$/,':'+add)
}

alert(newString( "newSegName","seg1:seg2:seg3:seg4"))

</script>
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now