最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Matching multiple occurrences of consecutive characters from a set - Stack Overflow

matteradmin6PV0评论

I am trying to match 3 or more consecutive occurrences of one/more special characters from a set in a string.

my js:
var exp = new RegExp("^[\^\$\*%#`!]{3}+$"); 
return !exp.test(myString);

Not working. Any help will be appreciated.

this i$ a te$t: false
th!$ a te$t: false

th!s is a ^%* test: true 
as it has 3 consecutive characters from my set '^%*'

this is a ^%^&%&%& test: true
as it has 3 or more consecutive characters from my set

EDIT:

I corrected the regex. This in JavaScript

var exp = new RegExp("[\^$%#`]{3}"); return exp.test(myString);

Why it is saying this is a match? %^ $^ $^ $^ &^ &^ & %

This input has 2 characters at most and has space in between.

I am trying to match 3 or more consecutive occurrences of one/more special characters from a set in a string.

my js:
var exp = new RegExp("^[\^\$\*%#`!]{3}+$"); 
return !exp.test(myString);

Not working. Any help will be appreciated.

this i$ a te$t: false
th!$ a te$t: false

th!s is a ^%* test: true 
as it has 3 consecutive characters from my set '^%*'

this is a ^%^&%&%& test: true
as it has 3 or more consecutive characters from my set

EDIT:

I corrected the regex. This in JavaScript

var exp = new RegExp("[\^$%#`]{3}"); return exp.test(myString);

Why it is saying this is a match? %^ $^ $^ $^ &^ &^ & %

This input has 2 characters at most and has space in between.

Share Improve this question edited Mar 29, 2012 at 18:44 kheya asked Mar 29, 2012 at 18:29 kheyakheya 7,62120 gold badges79 silver badges115 bronze badges 2
  • your regex is invalid, you can't have the + after the {3} that's like saying "I want it exactly 3 times, but also 1 or more times is acceptable." – Robbie Commented Mar 29, 2012 at 18:35
  • in your update, that is matching because you have a space in the character set.... check my answer below... i've tested it and it seems to work – Robbie Commented Mar 29, 2012 at 18:50
Add a ment  | 

4 Answers 4

Reset to default 3
new RegExp("[\^\$\*%#`!]{3}");

The leading ^ indicated that the pattern must start at the beginning of the string. The trailing $ indicated that the pattern must end at the end of the string. The bination of these would indicate that the only valid match is a string consisting only of your special characters.

Finally, the + isn't necessary, at soon as you find the first 3 consecutive special characters, you're done; that it can match 5 is irrelevant.

I guess you want

/[\^$*%#`!]{3,}/
== new RegExp("[\\^$*%#`!]{3,}")

Your string should have a occurence of these characters, not consist out of them. Also, .{3}+ seems odd to me.


EDIT (to answer your extended question):

Also, if you want to escape the ^ with a backslash, you will have to escape the backslash itself in the string for your RegExp constructor. Your new RegExp("[\^$%#']{3}") equals /[^$%#']{3}]/, which matches the sequence "^ &".

You're looking for only 3 characters between the ^ and $ of the string. You'll want to pad on either end with .*? (or just leave out the ^ and $).

http://regexpal./ is always helpful.

Try this:

Regex:

/[\^$*%#`!]{3,}/

Javascript tests:

var result = new RegExp(/[\^$*%#`!]{3,}/).test("%^ $^ $^ $^ &^ &^ & %"); 
// result now "false"
new RegExp(/[\^$*%#`!]{3,}/).test("th!s is a ^%* test"); 
// result is now "true"

I've removed the invalid + and some unnecessary escape chars. Oh, and as suggested by the other users, the Anchors ^$ are not needed .

You could however wrap the regex in \b tags if you wanted to limit the matches to word boundaries (so that it doesn't match in the middle of a set of characters - only if the set is on its own)

Post a comment

comment list (0)

  1. No comments so far