Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace last segment of string

Posted on 2009-07-02
4
Medium Priority
?
212 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

885 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