Solved

Replace last segment of string

Posted on 2009-07-02
4
202 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 31
validation, if text area is typed or pasted into 2 25
How to drill down using class name and find the text? 25 32
Form with Modals 16 42
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

914 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

19 Experts available now in Live!

Get 1:1 Help Now