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?

Share Improve this question edited May 23, 2017 at 12:20 CommunityBot 11 silver badge asked Mar 6, 2014 at 4:53 user1952811user1952811 2,4686 gold badges33 silver badges49 bronze badges 1
  • Regex.Replace(inputString, "-+", "") bees inputString.replace(/-+/g, ""). Trim bees trim. The vars work as they are. – Ry- Commented Mar 6, 2014 at 4:56
Add a ment  | 

6 Answers 6

Reset to default 8

In 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