RegEx 允许在开头使用字母数字,然后使用连字符 [英] RegEx for allowing alphanumeric at the starting and hyphen thereafter

查看:46
本文介绍了RegEx 允许在开头使用字母数字,然后使用连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,其中文本字段中的第一个字符应该是字母数字然后我可以在 JavaScript 中允许此后的连字符.最后也不应允许连字符

解决方案

如果不想匹配多个破折号:

<前>^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

这将匹配:aaaaaa-aaaa-a-aaa-a-aaa-a
但不是:--aa-a--aaaa-aa--a

I had a requirement where in the text field the first character should be a alpha numeric and then i can allow a hyphen from thereafter in JavaScript.Also hyphen should not be allowed at the end

解决方案

If you do not want to match mutiple dashes after eachother:

^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$

This will match: a, a-a, aaa-a, aaa-a-aaa-a-aaa-a, etc
But not: -, -a, a-, a--a, a-a-a-, a-a--a, etc.

这篇关于RegEx 允许在开头使用字母数字,然后使用连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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