5

I have a string value which would look like the following with sample

data:


130823  ~ Optics-Bottle Detect failure ~ L 0 ~ P 0 | 130824 ~ Optics-Bubble Detect failure ~ L 0 ~ P 0

Format:

ID:          130823
Description: Optics-Bottle Detect failure 
Reps:        L O
Pending:     P 0

My final string should basically remove the ID from each part in the concatenated string so by looking at the above sample data the desired output should be as follows:

 Optics-Bottle Detect failure ~ L 0 ~ P 0 | Optics-Bubble Detect failure ~ L 0 ~ P 0 

There could be N number of parts in one string. For the sake of example I only included a sample string which has two parts in it.

**My Regex

Im using the following regular expression but it only removed the ID from the first part in the string

var y = x.replace(/\d{6}\s~\s/g, "");
6
  • 2
    This is removing both IDs from the string above for me. Commented Jul 2, 2013 at 21:35
  • That removes both: jsfiddle.net/6KkGK Commented Jul 2, 2013 at 21:35
  • It works for me: jsfiddle.net/bbsss Commented Jul 2, 2013 at 21:35
  • @RocketHazmat I corrected my sample data string. There was an issue earlier. With this sample data it will not return correctly Commented Jul 2, 2013 at 21:45
  • @nnnnnn I corrected my sample data string. There was an issue earlier. With this sample data it will not return correctly Commented Jul 2, 2013 at 21:45

2 Answers 2

4

Here's a possible answer, for example

var str = "144515 ~ Commodities-Damaged Reagent Cartridge ~ L 0 ~ P 0 | 144516 ~ Commodities";
var n=/\d{6}\s+~/g;
str = str.replace(n, "");
Sign up to request clarification or add additional context in comments.

1 Comment

This does not remove the ~.
1

Split the string into a multidimensional array using the delimiters " | " and " ~ ". Then you can .shift() off the id, since it is the first entry in the array, and join it all back together:

var y = x.split(" | ").map(function(s) {
    s = s.split(" ~ ");
    s.shift();
    return s.join(" ~ ");
}).join(" | ");

Or, get the substring after the " ~ ":

var y = x.split(" | ").map(function(s) {
    return s.substr(s.indexOf(" ~ ") + 3);
}).join(" | ");

Or, correct your RegExp to account for whitespace length variation:

var y = x.replace(/\d{6}\s+~\s/g, "");

But, this RegExp will only work as long as there are always exactly 6 digits in the id, and never 6 digits preceding a ~ elsewhere. For example, if there should ever be a value for Reps of 100000 or more, your RegExp will remove that as well.

A better regular expression would take any number of digits (more or less than 6) and would only match it if it's the first item, or follows a |:

var y = x.replace(/(^|\|\s+)\d+\s+~\s+/g, "$1");

2 Comments

The ids could go past 6 digits very well since its a identity column value for rows. Should I just use the first function you have listed in the answer above.
Yes, either of the first two solutions would work better, assuming id always comes first in each entry. I'll update my answer with a more robust regular expression option as well, if you prefer that option.

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.