最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex to validate entire multiline text in angularjs - Stack Overflow

programmeradmin3浏览0评论

I need some help.
I have to construct a regex for angularjs ng-pattern attribute. The regex has to validate a text, not each line or some pieces. The text has to contains some amounts with exactly 2 decimals and each amount should be entered in the new line. Also, spaces are accepted before and after each amount. If one line contains 2 amount then the entire text is not valid.

For example this text is valid because each amount is entered in the new line:

123.34 
12345.56
2.54

This example is not valid because one line contains 2 amounts:

12.43
123.32 2345.54
124.43

This example is not valid because one amount does not contains 2 decimal(each amounts has to be with exactly 2 decimals):

123
123.43
123.65

My best try is ^(([0-9]+[.][0-9]{2})\s*)+$ and it can be tested here. But my regex it's not enough because it accept text with multiple amounts in the same line.

Thanks

I need some help.
I have to construct a regex for angularjs ng-pattern attribute. The regex has to validate a text, not each line or some pieces. The text has to contains some amounts with exactly 2 decimals and each amount should be entered in the new line. Also, spaces are accepted before and after each amount. If one line contains 2 amount then the entire text is not valid.

For example this text is valid because each amount is entered in the new line:

123.34 
12345.56
2.54

This example is not valid because one line contains 2 amounts:

12.43
123.32 2345.54
124.43

This example is not valid because one amount does not contains 2 decimal(each amounts has to be with exactly 2 decimals):

123
123.43
123.65

My best try is ^(([0-9]+[.][0-9]{2})\s*)+$ and it can be tested here. But my regex it's not enough because it accept text with multiple amounts in the same line.

Thanks

Share Improve this question edited Feb 2, 2018 at 9:16 31piy 23.9k6 gold badges51 silver badges68 bronze badges asked Feb 2, 2018 at 9:10 Gryph GGryph G 1579 bronze badges 5
  • Can there be leading / trailing spaces on the lines? Try ^\d+\.\d{2}(?:\r?\n\d+\.\d{2})*$ if not, see regex101./r/E5l3SB/1 demo. – Wiktor Stribiżew Commented Feb 2, 2018 at 9:19
  • Yes, leading / trailing spaces are accepted. – Gryph G Commented Feb 2, 2018 at 9:21
  • 1 Then try /^[^\S\r\n]*\d+\.\d{2}(?:[^\S\r\n]*\r?\n[^\S\r\n]*\d+\.\d{2})*[^\S\r\n]*$/. – Wiktor Stribiżew Commented Feb 2, 2018 at 9:25
  • Thank you WiktorStribiżew, it is perfect. – Gryph G Commented Feb 2, 2018 at 9:49
  • I posted the answer explaining the regex and explained it together with how you may tweak it and how it interacts with ng-trim. – Wiktor Stribiżew Commented Feb 2, 2018 at 10:07
Add a ment  | 

4 Answers 4

Reset to default 6

Regex is not my strong point, so there may be a much simpler way to do this, but this does meet your requirements:

^(([^\S\r\n]*[0-9]+[.][0-9]{2}[^\S\r\n]*)\n)*(([^\S\r\n]*[0-9]+[.][0-9]{2}[^\S\r\n]*))$

Effectively what it does is ensure that the last line (without a newline character at the end) is always present, but also allows for optional lines before that which end with a newline (\n).

We also use the [^\S\r\n] part in place of \s to ensure that it checks for whitespace characters excluding newline, as the newline is what causes an issue with validating multiple values on the same line.

Here is a working example

You may use

ng-pattern="/^[^\S\r\n]*\d+\.\d{2}(?:[^\S\r\n]*\r?\n[^\S\r\n]*\d+\.\d{2})*[^\S\r\n]*$/"

See the regex demo.

Or, since angular trims the input before sending it to the regex engine by default (if you have no ng-trim or if you have ng-trim="true"), you may also use

ng-pattern="/^\d+\.\d{2}(?:[^\S\r\n]*\r?\n[^\S\r\n]*\d+\.\d{2})*$/"

Or, if you want to make sure there are no empty lines at start/end, use the first regex with ng-trim="false".

Pattern details

  • ^ - start of string
  • [^\S\r\n]* - any 0+ horizontal whitespaces
  • \d+ - 1 or more digits
  • \. - a . char
  • \d{2} - 2 digits
  • (?: - start of a non-capturing group matching...
    • [^\S\r\n]* - any 0+ horizontal whitespaces
    • \r?\n - a line break sequence
    • [^\S\r\n]*\d+\.\d{2} - any 0+ horizontal whitespaces, 1+ digits, . and 2 digits
  • )* - ... zero or more times
  • [^\S\r\n]* - any 0+ horizontal whitespaces
  • $ - end of string.

add the multiline flag /m to be able to anchor ^ to the start of each line

/^([0-9]+[.][0-9]{2})\s*$/gm

e.g. https://regex101./r/XMTSok/4

the problem is that \s matches also spaces use [\r\n] instead to match newlines and cariage return.

EDIT my first post mentioned \R but it's PCRE specific.

EDIT following ments to accept also space at the end of line

^(([0-9]+[.][0-9]{2}) *(?:\r?\n|$))+$

regex101

发布评论

评论列表(0)

  1. 暂无评论