XJS: extract string within a string

Posted on 2014-12-04
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 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("/");

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 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


Open in new window

to see the new string.
Technology Partners: 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 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

Author Comment

ID: 40482971
yes that works. thanks so much

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handling case and special names in javascript 3 50
Make Float not to Wrap 15 65
Set css in function 11 54
Jquery Calculation 3 40
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

737 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