admin管理员组文章数量:1435859
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
4 Answers
Reset to default 3new 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)
本文标签: javascriptMatching multiple occurrences of consecutive characters from a setStack Overflow
版权声明:本文标题:javascript - Matching multiple occurrences of consecutive characters from a set - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745645685a2668120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论