javascript - Can we use regex to resolve multiple rules at once? - Stack Overflow

I am in need to prevent some user input interaction and on top of my mind is regex immediately.I'l

I am in need to prevent some user input interaction and on top of my mind is regex immediately.

I'll first write as best as I can what I need to acplish and then I'll post the code that I wrote to acplish most of these, but still not perfect...

  • Take the string
  • Do not allow it to start typing with a space
  • Do not allow multiple spaces continuously
  • Do not allow multiple dots continuously
  • Do not allow multiple apostrophies
  • Allow following characters [a-z] '.
  • Do not allow string to cross over 14 characters

Now, here's my code:

this.name = this.name
  .replace(/^[\s]+/, '')     // prevent starting with space
  .replace(/\s\s+/, ' ')     // prevent multiplace spaces
  .replace(/\.\./, '.')      // prevent multiple dots
  .replace(/''/, '\'')       // prevent multiple apostrophies
  .replace(/[^ a-z'.]/i, '') // allowed
  .toUpperCase()             // transform
  .substring(0, 14)          // do not allow more than 14 characters

Questions:

  • Can we and how if so, acplish all those or most of these regex rules in single replace?

  • How can I fix/improve my regex rules to not allow more than single ., I made it so that it does not allow continuous entries, but a user can use two dots like M.G.K - even tho I want to allow only single . entry in whole string?

  • Same as above, but for ' (apostrophe)?

I am in need to prevent some user input interaction and on top of my mind is regex immediately.

I'll first write as best as I can what I need to acplish and then I'll post the code that I wrote to acplish most of these, but still not perfect...

  • Take the string
  • Do not allow it to start typing with a space
  • Do not allow multiple spaces continuously
  • Do not allow multiple dots continuously
  • Do not allow multiple apostrophies
  • Allow following characters [a-z] '.
  • Do not allow string to cross over 14 characters

Now, here's my code:

this.name = this.name
  .replace(/^[\s]+/, '')     // prevent starting with space
  .replace(/\s\s+/, ' ')     // prevent multiplace spaces
  .replace(/\.\./, '.')      // prevent multiple dots
  .replace(/''/, '\'')       // prevent multiple apostrophies
  .replace(/[^ a-z'.]/i, '') // allowed
  .toUpperCase()             // transform
  .substring(0, 14)          // do not allow more than 14 characters

Questions:

  • Can we and how if so, acplish all those or most of these regex rules in single replace?

  • How can I fix/improve my regex rules to not allow more than single ., I made it so that it does not allow continuous entries, but a user can use two dots like M.G.K - even tho I want to allow only single . entry in whole string?

  • Same as above, but for ' (apostrophe)?

Share Improve this question edited Sep 1, 2017 at 16:00 Tyler Roper 21.7k6 gold badges35 silver badges58 bronze badges asked Sep 1, 2017 at 15:45 dvldendvlden 2,4628 gold badges39 silver badges61 bronze badges 8
  • 1 Try s.replace(/^\s+|s(\s)+|(\.)\.|(')'|[^ a-z'.\n]+/g, '$1$2$3').toUpperCase().substring(0, 14), but the last 2 points are out of scope for a one-pass JS regex. – Wiktor Stribiżew Commented Sep 1, 2017 at 15:53
  • @WiktorStribiżew weird, but when I start typing, it allows me to type single letter and then it removes all – dvlden Commented Sep 1, 2017 at 15:57
  • I would remend readability over cramming a pile of rules together. Later when you are trying to figure out why it doesn't work and you have a very plicated regex it will be hard. If you several lines nicely mented it will be easy. I suspect performance won't be much different. – sniperd Commented Sep 1, 2017 at 16:05
  • @sniperd Well thanks for the information. Still, it's nice to learn that it can be done from Wiktor's sample by |. However, I still do need some help improving and learning how to improve my regex rules, as they do not really acplish what I want to achieve – dvlden Commented Sep 1, 2017 at 16:08
  • 1 Yes, I made some typos. You need 2 more regex replacements to implement the lsast two requirements. Something like .replace(/^([^']*'[^']*)'/, '$1').replace(/^([^.]*\.[^.]*)\./, '$1') – Wiktor Stribiżew Commented Sep 1, 2017 at 16:33
 |  Show 3 more ments

1 Answer 1

Reset to default 6

The first requirement to "cram" everything into 1 regex replace operation is rather easy to implement, because all the replacements you need to do are using the chars that you match. You may use .replace(/^\s+|(\s)\s+|(\.)\.|(')'|[^ a-z'.]+/ig, '$1$2$3'), see this regex demo.

Details

  • ^\s+ - start of string (^) and then 1+ whitespaces
  • | - or
  • (\s)\s+ - a single whitespace captured into Group 1 and then 1+ whitespace chars
  • | - or
  • (\.)\. - a . captured into Group 2, and then a .
  • | - or
  • (')' - a ' captured into Group 3 and then a '
  • | - or
  • [^ a-z'.]+ - 1 or more chars other than a space, ASCII letter, ' and ..

The /i modifier makes a-z match in a case insensitive way, g enables multiple matching. The $1 refers to the value in Group 1, $2 references the value in Group 2 and the $3 refers to the Group 3 value. Note that if they are not matched, these values in groups are empty strings, thus we may use three of them together in a single string replacement pattern.

The second and third requirements require two more separate regex replace operations. The point is to match and capture all chars up to the second occurrence of ' and . and just match the second occurrence of ' and ., and then replace with the backreference to the first group: 1) .replace(/^([^']*'[^']*)'/, '$1') (demo) and 2) .replace(/^([^.]*\.[^.]*)\./, '$1') (demo).

Details

  • ^ - start of string anchor
  • ([^']*'[^']*) - Group 1:
    • [^']* - any 0+ chars other than ' (a [^...] is a negated character class that matches any chars other than defined inside the class)
    • ' - a single quote
    • [^']* - any 0+ chars other than '
  • ' - a single quote.

This match is replaced with $1, the contents of the first capturing group.

The third pattern is analogous to the second parttern, just ' is replaced with . / \. (note that inside a character class, a . is treated as a literal ., it does not match any char but line break chars inside [...]).

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信