Solved

XJS: extract string within a string

Posted on 2014-12-04
6
142 Views
Last Modified: 2014-12-05
I have the following variable
var Text = nodeRQ.ValueOfSelect("Text");

This contains
A/ONE/TWO/MAIN TEST EXTRA/2//15D/738-738//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01

how do i change this such that it reads as follows. where i removed 738-738
A/ONE/TWO/MAIN TEST EXTRA/2//15D///1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01


so remove the contents between the 7th "/" and 8th "/" ?
Basically, using indexOf to find 7th "/" and 8th "/" then concatenate the remaining strings?
0
Comment
Question by:badtz7229
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 500 total points
ID: 40481728
you could try something like this:

var s = "A/ONE/TWO/MAIN TEST EXTRA/2//15D/738-738//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01";
var arr = s.split("/");
arr.splice(7,1);
arr.join("/");

Open in new window


->
"A/ONE/TWO/MAIN TEST EXTRA/2//15D//1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01"
0
 

Author Comment

by:badtz7229
ID: 40481816
@Kyle Hamilton - this seems to work. except the split function is replacing all the "/" with commas.
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 500 total points
ID: 40481829
no, the split function creates an array out of the string. you are seeing commas, because that is how an array is represented.

once you join the array back into a string, you will not see the commas anymore.

for clarity, you could change the line at the end to:

var newString = arr.join("/");

Open in new window


console.log(newString)

Open in new window


to see the new string.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:badtz7229
ID: 40482846
@Kyle Hamilton - that worked.
Only thing I just noticed is that when you remove the contents between the 7th "/" and 8th "/" , it's also removing the last "/". I want to maintain all "/"

so that it's

"A/ONE/TWO/MAIN TEST EXTRA/2//15D///1488/2/USD/TAX/112/US/1600///ADT/NNN//ZA/1//T/ZZ/ZZ///01"
0
 
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 500 total points
ID: 40482874
oh yeah. that's because we are removing the element. you can do this instead:

arr.splice(7,1, "");

add an empty element, to preserve the slash
0
 

Author Comment

by:badtz7229
ID: 40482971
yes that works. thanks so much
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…

706 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

15 Experts available now in Live!

Get 1:1 Help Now