javascript - Regex - match range of characters with a quantifier that only matches numbers - Stack Overflow

I've found a similar question on SO, but nothing I can get my head around. Here's what I need

I've found a similar question on SO, but nothing I can get my head around. Here's what I need;

6 or more digits, with these characters allowed \s\-\(\)\+

So here's what I have /^[0-9\s\-\(\)\+]{6,}$/

The problem is, I don't want anything other than the number to count towards the 6 or more quantifier. How can I only "count" the digits? It would also been good if I could stop those other allowed characters from being entered adjacently e.g:

0898--234 
+43 34  434

After an hour of reading up and looking at a regex cheat sheet, I'm hoping some kind person can point me in the right direction!

I've found a similar question on SO, but nothing I can get my head around. Here's what I need;

6 or more digits, with these characters allowed \s\-\(\)\+

So here's what I have /^[0-9\s\-\(\)\+]{6,}$/

The problem is, I don't want anything other than the number to count towards the 6 or more quantifier. How can I only "count" the digits? It would also been good if I could stop those other allowed characters from being entered adjacently e.g:

0898--234 
+43 34  434

After an hour of reading up and looking at a regex cheat sheet, I'm hoping some kind person can point me in the right direction!

Share Improve this question edited Nov 19, 2013 at 15:10 p.s.w.g 149k31 gold badges305 silver badges337 bronze badges asked Nov 19, 2013 at 15:02 Dan Dan 5604 gold badges10 silver badges21 bronze badges 5
  • 1 You don't need to escape special chars in a char class : [0-9\s\-\(\)\+] bee [\d\s()+-] (You just need to put the dash - at the end) – zessx Commented Nov 19, 2013 at 15:07
  • Ah, would I only escape them if they were being caught in a group (parenthesis)? Also, why must the dash go at the end? – Dan Commented Nov 19, 2013 at 15:30
  • 1 @Dan A hyphen is one of the few characters that may have a special meaning inside a character class: a range. By putting it at the end, it can't be denoting a range so it has no special meaning (i.e., it's treated literally) and needs no escaping. You don't have to put it at the end, but if you don't, you should escape it to avoid accidentally specifying a range. – Wiseguy Commented Nov 19, 2013 at 15:43
  • @Wiseguy - thanks, I should have seen that one ing, but sometimes regular expressions are pretty tough, especially when you don't write them for a few weeks. – Dan Commented Nov 19, 2013 at 15:47
  • 1 @Dan Example to understand possible issues with hyphen and closing bracket : [0-1] vs [01-] / [}])] vs []})]. – zessx Commented Nov 19, 2013 at 16:03
Add a ment  | 

3 Answers 3

Reset to default 4

You could do something like this:

/^([\s()+-]*[0-9]){6,}[\s()+-]*$/

This will match any number of special characters (whitespace, parentheses, pluses or hyphens) followed by a single decimal digit, repeated 6 or more times, followed by any number of special characters.

Or this if you don't want to match two or more adjacent special characters:

/^([\s()+-]?[0-9]){6,}[\s()+-]?$/

You can use lookahead:

/^(?=(\D*\d){6,})[0-9\s()+-]{6,}$/
/^[\s()+-]*(\d[\s()+-]*){6,}$/

This doesn't count the 'cruft'. It allows any number of special characters, followed by six times [a digit followed by any number of special characters].
If you want max. one special character in between digits, use ? instead of *, but I assume you don't care much for more than one special character at the start or at the end, so I'd go with

/^[\s()+-]*(\d[\s()+-]?){6,}[\s()+-]*$/

This matches any number of special characters, followed by 6 or more times [a digit followed by at most one special character], followed by any number of special characters.


Another option would be to strip your special characters from the string first, and then match against 6 or more digits.

var rawInput = "    12  (3) -- 4 -5 ++6";
var strippedInput = rawInput.replace(/[\s()+-]*/g, "");
return new RegExp("^\d{6,}$").test(strippedInput);

Remember that you have a plete programming language at your disposal. I've noticed people tend to decide they need to use regex and then forget about everything else they can do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204922a4616547.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信