Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace last segment of string

Posted on 2009-07-02
4
Medium Priority
?
209 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
[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
4 Comments
 
LVL 5

Assisted Solution

by:Opalelement
Opalelement earned 800 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 1200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

722 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