Limit the number of lines or words using regex in Google forms

大憨熊 提交于 2020-01-07 04:36:05

问题


Does anybody know any regex to restrict a string to specified number of lines and words for data validation of a field in google forms?

I have already tried the following expression

^(?:\b\w+\b[\s\r\n]*){1,250}$

That would limit to 250 words over multiple lines.

but it is not working when there is any special character in the string. Any help would be appreciated.


回答1:


Your [\s\r\n] matches any whitespace chars only. You may use \W to match any non-word char:

^\w+(?:\W+\w+){0,249}$
       ^^

Details:

  • ^ - start of string
  • \w+ - 1+ word chars
  • (?:\W+\w+){0,249} - 0 to 249 (1 to 250 in total) sequences of:
    • \W+ - 1+ non-word chars
    • \w+ - 1+ word chars (that is, words separated with 1+ symbol, whitespace or punctuation)
  • $ - end of string.


来源:https://stackoverflow.com/questions/43889293/limit-the-number-of-lines-or-words-using-regex-in-google-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!