AngularJS - 使用正则表达式从输入框中删除前导和尾随空格 [英] AngularJS - Remove leading and trailing whitespace from input-box using regex

查看:21
本文介绍了AngularJS - 使用正则表达式从输入框中删除前导和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个正则表达式(regex),用于在具有以下条件的输入框中添加多个电子邮件 ID:

I am writing a regular expression(regex) for adding multiple email ids in an input box with following conditions:

  1. 多个电子邮件 ID 必须用逗号分隔 ,
  2. 需要至少有一个电子邮件 ID
  3. 输入字段中不应有任何空格.

所以我创建了这个正则表达式:

So i created this regex:

^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$

我在 regex101.com 中对其进行了测试,并且它的工作原理非常棒https://regex101.com/r/bU7rU8/1

I tested it in regex101.com and its working like a charm https://regex101.com/r/bU7rU8/1

但是当我将它与代码集成时,它可以工作,但在前导和尾随空格上失败.

But when i integrate it with code, it works, but fails on leading and trailing whitespace.

这是演示链接:http://jsfiddle.net/2G8gA/330/

推荐答案

AngularJS 默认修剪输入,所以需要使用 ng-trim="false" 来传递前导和pattern 正则表达式的尾随空格.

AngularJS trims the input by default, so you need to use ng-trim="false" in order to pass leading and trailing whitespace to your pattern regex.

请参阅文档:

ngTrim(可选)

如果设置为 false Angular 将不会自动修剪输入.input[type=password] 控件将忽略此参数,该控件永远不会修剪输入.

If set to false Angular will not automatically trim the input. This parameter is ignored for input[type=password] controls, which will never trim the input.

(默认:true)

这篇关于AngularJS - 使用正则表达式从输入框中删除前导和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆