admin管理员组文章数量:1315851
I've seen the following question Regex to remove a specific repeated character which is extremely similar to mine (if not exact) but it's implemented in C# and using that language's string methods.
I am wondering if someone could e up with a javascript
implementation of it?
Example if you have
what---is-your-name- => what-is-your-name
---what-is----your-name-- => what-is-your-name
So how to remove the recurrence of a specific character, in this case -
and replace it with just one -
in javascript?
I've seen the following question Regex to remove a specific repeated character which is extremely similar to mine (if not exact) but it's implemented in C# and using that language's string methods.
I am wondering if someone could e up with a javascript
implementation of it?
Example if you have
what---is-your-name- => what-is-your-name
---what-is----your-name-- => what-is-your-name
So how to remove the recurrence of a specific character, in this case -
and replace it with just one -
in javascript?
-
Regex.Replace(inputString, "-+", "")
beesinputString.replace(/-+/g, "")
.Trim
beestrim
. Thevar
s work as they are. – Ry- ♦ Commented Mar 6, 2014 at 4:56
6 Answers
Reset to default 8In one shot:
str.replace(/^-+|-+(?=-|$)/g, '')
explanations:
(?=..)
is a lookahead assertion and means followed by. It is only a check and is not a part of the match result.
about -+(?=-|$)
:
Since quantifiers are greedy by default -+
matches all -
in a part of the string, then the lookahead is tested: two possible situations
I. The part is in the middle of the string: hello-----world
since there is only a w
after the lookahead fails and the regex engine goes one character back. Now -+
matches only four -
that is followed by a -
, the lookahead success. Since it is not a part of the match, the last -
is not removed by the replace function.
II. The part is at the end of the string: world-----
-+
matches all -
until the end of the string and the second part of the lookahead assertion success. All -
are removed by the replace function.
Note that (?=-|$)
(followed by a dash or the end of the string) can also be written (?![^-])
(not followed by any character that is not a dash) to avoid the alternation. It's the same but expressed in a negative way.
something like this
var str = "---what---is-your----name-----";
var res1 = str.replace(/^-+/,'');
console.log(res1);
var res2 = res1.replace(/-+$/,'');
console.log(res2);
var res3 = res2.replace(/-+/g,'-');
console.log(res3);
or you can simply put all the conditions into one for a one liner
str.replace(/^-+|-+$|-+/g,'-');
Use this regexp
.replace(/\-+/g,'-').replace(/^-|-$/g, '');
This should do it:
var newValue = value.replace(/-+/g, '-');
Your example provided contradicts your description of the problem, since it looks like you want to remove all hyphens at the beginning and end. If so, this will do it:
var newValue = value.replace(/^-+|-+$/g, '').replace(/-+/g, '-');
Here is a variation answer which only uses a single regular expression and one call to String.replace
; other answers still apply.
s = s.replace(/^-*|-*$|(-)-*/g, "$1");
So:
s = "---what-is----your-name--";
s = s.replace(/^-*|-*$|(-)-*/g, "$1");
// s == what-is-your-name
Explanation:
^-* // match any number of dashes at the start
-*$ // match any number of dashes at the end
(-)-* // match one or more dashes, capturing one dash in 1st group
/g // match globally/repeatedly
"$1" // replace with 1st group value;
// so it will replace with "-" or "" (for undefined capture)
"what---is-your-name-".replace(/\-+/g, '-');
本文标签: regexDelete a specific repeated character in javascriptStack Overflow
版权声明:本文标题:regex - Delete a specific repeated character in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741986140a2408690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论