admin管理员组

文章数量:1355721

Suppose I have a string

",,,a,,,,,b,,c,,,,d,,,,"

I want to convert this into

"a,b,c,d"

in 1 RegExp operation.

I can do it in 2 RegExp operations like

var str = ",,,a,,,b,,,,c,,,,,,,,d,,,,,,";
str = str.replace(/,+,/g,",").replace(/^,*|,*$/g, '');

is it possible to do this in 1 RegExp operation ?

Suppose I have a string

",,,a,,,,,b,,c,,,,d,,,,"

I want to convert this into

"a,b,c,d"

in 1 RegExp operation.

I can do it in 2 RegExp operations like

var str = ",,,a,,,b,,,,c,,,,,,,,d,,,,,,";
str = str.replace(/,+,/g,",").replace(/^,*|,*$/g, '');

is it possible to do this in 1 RegExp operation ?

Share Improve this question edited Jan 5, 2017 at 21:29 guest271314 102k15 gold badges117 silver badges187 bronze badges asked Jan 5, 2017 at 20:09 marvel308marvel308 10.5k3 gold badges23 silver badges32 bronze badges 6
  • "I want to convert this into "a,b,c,d" in 1 search I can do it in 2 searches" Can you describe precisely what "search" and "searches" is intended to mean at Question? – guest271314 Commented Jan 5, 2017 at 21:12
  • search basically means the number of RegExp operations required to convert the given string – marvel308 Commented Jan 5, 2017 at 21:19
  • "search basically means the number of RegExp operations required to convert the given string" Have you considered including that description at Question, for clarity? This means that Answer at stackoverflow./a/41493946 meets requirement of original Question, yes? – guest271314 Commented Jan 5, 2017 at 21:21
  • yes it does, I'll update the description – marvel308 Commented Jan 5, 2017 at 21:25
  • To be clear, am not asking for own Answer to be accepted, or even voted upon. Accept the Answer which bests meets requirement. Just pointing out that text of original Question was not clear as to what "search" and "searches" were intended to mean as to requirement. – guest271314 Commented Jan 5, 2017 at 21:27
 |  Show 1 more ment

2 Answers 2

Reset to default 13

You could use a regular expression, which are at start or are followed by a ma or at the and and replace it with an empty string.

/^,*|,(?=,|$)/g

  • 1st Alternative ^,*

    ^ asserts position at start of the string

    • ,* matches the character , literally (case sensitive)

      * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

  • 2nd Alternative ,+(?=,|$)

    • ,+ matches the character , literally (case sensitive)

      + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

    • Positive Lookahead (?=,|$)

      Assert that the Regex below matches

      • 1st Alternative ,

        , matches the character , literally (case sensitive)

      • 2nd Alternative $

        $ asserts position at the end of the string

  • Global pattern flags

    g modifier: global. All matches (don't return after first match)

var string =  ",,,a,,,,,b,,c,,,,d,,,,";

console.log(string.replace(/^,*|,+(?=,|$)/g, ''));

The approach below returns expected result using two processes. .match() and template literal, which casts the encapsulated javascript expression to string when assigned to a variable.

You can use String.prototype.match() with RegExp /[^,]+/ to negate matching ma character ,, match one or more characters other than ,, including + in RegExp following character class where , is negated to match "abc" as suggested by @4castle; template literal to cast resulting array to string.

var str = ",,,a,,,b,,,,c,,,,,,,,d,,,efg,,,";

str = `${str.match(/[^,]+/g)}`;

console.log(str);

本文标签: