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 ?
- "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
2 Answers
Reset to default 13You 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 stringGlobal 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);
本文标签:
版权声明:本文标题:javascript - Merge contiguous "," into a single "," and remove leading and trailing &quo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743977140a2570923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论