简体   繁体   English

如何在此正则表达式中包含 - 和 '?

[英]How do I include - and ' in this regular expressions?

I have a this regular expression below for some input name fields.对于一些输入名称字段,我在下面有一个这个正则表达式。 How do I include an apostrophe and a hyphen in this?我如何在其中包含撇号和连字符?

InputField("tFName", /^[a-zA-Z-\-\ ]+$/);

Hyphen is already included (twice), you can add the apostrophe by just editing it into the character class:已包含连字符(两次),您可以通过将其编辑到字符类中来添加撇号:

/^[a-zA-Z-\-\ ']+$/

You can rewrite it to look like this, so that there's no need to escape the hyphen and it's only included once:您可以将其重写为如下所示,这样就无需转义连字符,它只包含一次:

/^[a-zA-Z '-]+$/

Example: http://jsfiddle.net/a4vGA/示例: http : //jsfiddle.net/a4vGA/

Try this:试试这个:

"abc'def ghi-jkl mno-pq'rst".match(/^[\w\s-']+$/)
  • \\w for any letter \\w任何字母
  • \\s for space \\s表示空间
  • - for hyphen -对于连字符
  • ' for apostrophe '为撇号

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

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