Solved

Replace last segment of string

Posted on 2009-07-02
4
206 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
ID: 24769344
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
ID: 24769580
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
ID: 24769636
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
ID: 24769968
try this

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

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

</script>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 38
Jquery Datatables How can you add a custom column with glyphs for edit and delete for each row 7 31
Sending ASP to server side 8 28
Read text on Table 7 25
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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

679 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