1

I have a string in the following format. Im trying to create a function in Java Script to remove certain charcters.

Sample String:

Var s = '18160 ~ SCC-Hard Drive ~ 4 ~ d | 18170 ~ SCC-SSD ~ 4 ~ de | 18180 ~ SCC-Monitor ~ 5 ~ | 18190 ~ SCC-Keyboard ~ null ~'

Desired Result:

s = 'SCC-Hard Drive ~ 4 ~ d | SCC-SSD ~ 4 ~ de | SCC-Monitor ~ 5 ~ |SCC-Keyboard ~ null ~'

If you notice above the ID'S for instance 18160, 18170, 18180 and 18190 were removed. This is just as example. the structure is as follows:

id: 18160
description : SCC-Hard Drive
Type: 4
comment: d

So where there are multiple items they get concatenated using the Pike delimeter. So my requirement is to remove the id's from a given string in the above structure.

3
  • What are the requirements? Will that string change? Will the numbers always be the same? ie. 18***? Commented Mar 22, 2013 at 17:23
  • What defines the portions that need to be removed? Commented Mar 22, 2013 at 17:24
  • if they're all in the same format, i'd suggest running it through a regex Commented Mar 22, 2013 at 17:24

2 Answers 2

3

Using the string.replace() method perhaps.

s.replace(/\d{5}\s~\s/g, "")

\d{5} - matches 5 digits (the id)
\s    - matches a single space character
~     - matches the ~ literally

Output:

"SCC-Hard Drive ~ 4 ~ d | SCC-SSD ~ 4 ~ de | SCC-Monitor ~ 5 ~ | SCC-Keyboard ~ null ~"

Also, note that Var isn't valid. It should be var.

Sign up to request clarification or add additional context in comments.

Comments

1

I'll use the replace function with following regex since number of digits in the ID field can vary.

s.replace(/(^|\|\s)\d+\s~\s/g, '$1')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.