简体   繁体   English

带hypen和正则表达式的字符串javascript

[英]String with hypen and space regex javascript

I need to construct regex to validate the text it can contain white space and hyphen with no numbers and special character except hyphen. 我需要构造正则表达式以验证其可以包含空格和连字符的文本,除了连字符以外,没有数字和特殊字符。 Text should start with alphabets. 文字应以字母开头。 No starting and ending with white space and hyphen. 没有以空格和连字符开头和结尾。 Between the words single white space should be allowed. 单词之间应使用单个空格。

Ex text: 前文字:

  1. asdf-a adf-aad asdf-a adf-aad
  2. aaa a ddd-a aaa a ddd-a
  3. example ad-aa-a 范例ad-aa-a
  4. adf-ad-dd-a aaa adf-ad-dd-a aaa

This should do the trick. 这应该可以解决问题。

str.match(/^[a-z](?:([a-z-](?!--))+\s?(?!$))*(?:[a-z]*)$/)

That should give you a string that starts with a letter, then contains 1 or more letters or hyphens, followed by 1 optional space and then more words. 这应该给您一个以字母开头,然后包含1个或多个字母或连字符,然后是1个可选空格以及更多单词的字符串。 Also does not allow duplicate spaces. 也不允许重复的空格。 It appears to work for all the test cases I could think of: 它似乎适用于我能想到的所有测试用例:

JSFiddle: http://jsfiddle.net/tA4X9/1/ JSFiddle: http : //jsfiddle.net/tA4X9/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM