最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to use javascript regex to remove characters from string - Stack Overflow

programmeradmin0浏览0评论

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, "");

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, "");
Share Improve this question edited Jul 2, 2013 at 21:43 tam tam asked Jul 2, 2013 at 21:33 tam tamtam tam 1,9002 gold badges26 silver badges51 bronze badges 6
  • 2 This is removing both IDs from the string above for me. – Nick Tomlin Commented Jul 2, 2013 at 21:35
  • That removes both: jsfiddle/6KkGK – nnnnnn Commented Jul 2, 2013 at 21:35
  • It works for me: jsfiddle/bbsss – gen_Eric 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 – tam tam 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 – tam tam Commented Jul 2, 2013 at 21:45
 |  Show 1 more ment

2 Answers 2

Reset to default 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, "");

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");
发布评论

评论列表(0)

  1. 暂无评论