[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


XJS: extract string within a string

Posted on 2014-12-04
Medium Priority
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?
Question by:badtz7229
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
  • 3
  • 3
LVL 25

Accepted Solution

Kyle Hamilton earned 2000 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("/");

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"

Author Comment

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

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 2000 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


Open in new window

to see the new string.
Independent Software Vendors: 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!


Author Comment

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"
LVL 25

Assisted Solution

by:Kyle Hamilton
Kyle Hamilton earned 2000 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

Author Comment

ID: 40482971
yes that works. thanks so much

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Suggested Courses

656 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