[英]javascript regex [-_.] not first character
I do not want the first letter of these characters _-.
我不想要这些字符的第一个字母_-.
Used but can use between characters已使用但可以在字符之间使用
'(^[a-zA-Z0-9-_. ]*$){1,10}'
If [a-zA-Z0-9-. ]
如果[a-zA-Z0-9-. ]
[a-zA-Z0-9-. ]
is the allowed range of characters, should be present 1-10 times and can not start with -
or .
[a-zA-Z0-9-. ]
是允许的字符范围,应该出现 1-10 次,不能以-
或 开头.
then you don't need lookarounds.那么你不需要环顾四周。
You can match the first character [a-zA-Z0-9 ]
and repeat the fully allowed range 0-9 times.您可以匹配第一个字符[a-zA-Z0-9 ]
并重复完全允许的范围 0-9 次。
^[a-zA-Z0-9][a-zA-Z0-9. -]{0,9}$
See a regex demo .查看正则表达式演示。
If an underscore is also allowed, you could shorten the pattern using \w
:如果也允许使用下划线,则可以使用\w
缩短模式:
^[a-zA-Z0-9][\w. -]{0,9}$
I tweaked with some tools online and the follwoing do should do the job fine:我在网上使用了一些工具进行了调整,以下操作应该可以很好地完成工作:
const regex = /^[^\_\-\.]{1}(.)*/gm;
const str = `.awdawd`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
I also recommend using the website regex101.com if you wanted to play around with the Javscript regex.如果您想使用 Javscript 正则表达式,我还建议您使用网站 regex101.com。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.