我有一个表单,我想限制第一个和最后一个字符。(javascript)限制是:
示例:
Valid Entries: abc, ABC, Abc123, 123a, Abc_12, Abc_12_3a
Invalid Entries: _abc, abc_, _abc_
我尝试创建我的正则表达式如下:
^(?!\_)(?!_*_$)[a-zA-Z0-9_]+$
这不允许在开头加下划线,这很好,但允许在我想要限制的结尾加下划线。
这里缺少任何输入。
谢谢
const validEntries = ['abc', 'ABC', 'Abc123', '123a', 'Abc_12', 'Abc_12_3a'];
const invalidEntries = ['_abc', 'abc_', '_abc_'];
const regex = /^(?!\_)(?!_*_$)[a-zA-Z0-9_]+$/;
validEntries.forEach((x) => {
const ret = regex.test(x);
console.log('Should be true :', ret);
});
console.log('-------------------');
invalidEntries.forEach((x) => {
const ret = regex.test(x);
console.log('Should be false :', ret);
});
---更新---
我在angularjs应用程序中使用正则表达式。
我已将我的指令创建为:
.directive('restrictField', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ctrl) {
var regReplace,
preset = {
'alphanumeric-spl': '\\w_./\s/g',
'alphanumeric-underscore': '\\w_',
'numeric': '0-9',
'alpha-numeric': '\\w'
},
filter = preset[attrs.restrictField] || attrs.restrictField;
ctrl.$parsers.push(function (inputValue) {
regReplace = new RegExp('[^' + filter + ']', 'ig');
if (inputValue == undefined)
return ''
cleanInputValue = inputValue.replace(regReplace, '');
if (cleanInputValue != inputValue) {
ctrl.$setViewValue(cleanInputValue);
ctrl.$render();
}
return cleanInputValue;
});
}
}
})
我在我的html中使用它作为:
<input type="text" name="uname" ng-model="uname" required class="form-control input-medium" restrict-field="alphanumeric-underscore" ng-trim="false" />
您可以添加另一个负前瞻((?!.*u$)
)并使用
^(?!_)(?!.*_$)[a-zA-Z0-9_]+$
请参阅正则表达式演示。如果在除换行符之外的任何0个字符之后,字符串末尾有一个_
,(?!.* $)
前瞻将无法匹配。
或者,您可以使用无环视模式,如
^[a-zA-Z0-9]([a-zA-Z0-9_]*[a-zA-Z0-9])?$
^[a-zA-Z0-9](\w*[a-zA-Z0-9])?$
请参阅另一个正则表达式演示。此模式匹配:
^
-字符串的开始[a-zA-Z0-9]
-一个字母数字字符([a-zA-Z0-9_]*[a-zA-Z0-9])?
-可选序列
[a-zA-Z0-9_]*
-0个或多个单词字符(在JS中,您可以将其替换为\w*
)[a-zA-Z0-9]
-一个字母数字字符关于编辑
你的模式与描述不同步。考虑到预设应包含在替换方法中使用的模式,请尝试以下操作,记住匹配:
'alphanumeric-spl': '[^\\w\\s./]+', // Removes all but alnum, _ . / whitespaces
'alphanumeric-underscore': '^_+|_+$|_+(?=_)|\\W+', // Removes all _ at start/end and all _ before a _ and all but alnum and _
'numeric': '[^0-9]+', // Removes all but digits
'alpha-numeric': '[\\W_]+' // Removes all but alnum
然后
regReplace = new RegExp(filter, 'ig');
我正在使用正则表达式验证字符串(“-test-”)的开头和结尾是否包含hypens(-)。所以我找到了一个正则表达式来限制正则表达式开始和结束时的催眠。 当字符串包含多个带有或不带有hypen的字符(“aa”)时,该正则表达式按预期进行了验证。但是当我在没有hypen的情况下简单地传递一个字符串(“a”)时,它并没有像预期的那样工作。 此外,还需要允许特殊字符和字母数字字符,如“$abcd” 你们
我有一个正则表达式,它适用于 (字母数字-不分大小写) (仅限字母-与大小写无关) (只允许使用特殊字符和) null 我们是否可以修改上面的正则表达式来限制上面的两个测试用例?
我希望有一个正则表达式来检查字符串是否包含大小写字母、数字、下划线以及字符限制。这些是字符串中唯一允许的类型。
我想验证用户的输入,我使用以下(工作良好)作为regex。 但当我尝试所有我想要的角色时,就是这样。 它不起作用,我不知道为什么。此外,如果您能向我解释添加和有什么不同,我将不胜感激。另外,我尝试使用来代替空格,但仍然不起作用(我更喜欢使用空格,因为我想限制行的更改)。
问题内容: 如何在JavaScript中使用支持Unicode的正则表达式? 例如,应该有类似的东西可以匹配Letters或Marks类别中的任何代码点(而不仅仅是ASCII的),并且希望具有这样的过滤器来标点,等等。 问题答案: ES 6的情况 即将发布的ECMAScript语言规范,版本6,包含可识别Unicode的正则表达式。必须使用u正则表达式上的修饰符启用支持。请参阅ES6中支持Unic
正则练习 1.生成一个正则表达式regexObj 描述字符串规则的表达式,两种方式 直接量: /pattren/attrs(/规则/属性) 对象构造方式:new RegExp(pattern,arrtes) (/规则/属性) 2.regexObj.test(str) 测试正则表达式regexObj与指定字符串是否匹配 /10086/.test('10086') //true /10086/.tes