javascript - 正则表达式禁止特定序列中的某些字符

标签 javascript jquery regex

我有一个只允许字母、数字、空格或连字符的正则表达式。但是,我想禁止用户执行以下操作:

hello--world Have more than one hyphen sitting next to each other
--hello Have a hyphen in the beginning. It must have a number or letter first

我该如何实现?我当前的正则表达式如下所示:

let alphanumericTest = new RegExp("^\s*([0-9a-zA-Z- ]*)\s*$");

最佳答案

您可以试试这个正则表达式。 ^\s*[0-9a-zA-Z](?:(?!--)[0-9a-zA-Z- ])*$

This is a demo.

关于javascript - 正则表达式禁止特定序列中的某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369784/

相关文章:

php - 在php中使用preg_replace删除php中的空格

c# - 使用正则表达式 C# 在两个字符串的 block 中获取文本文件的多行字符串

javascript - 使用特定版本的 npm shrinkwrap

javascript - 无法选择类名为 : JQuery 的最后一个元素

javascript - 37 信号风格灯箱 |这个怎么做?

javascript - 需要 SelectBoxIt 和 ReactJS 支持

jquery - 将 jQuery 与 Typescript 2 结合使用

regex - 使用删除

javascript - jQuery Forms - Ajax 和普通提交相同的表单

javascript - 使用 JQuery 时如何首先加载 AngularJS