最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript regular expression ( alphanumeric characters with _-) - Stack Overflow

programmeradmin4浏览0评论

I am having trouble forming regular expression containing all alphanumeric characters and one or two specific characters, such us _ or -.

This expression works for all alphanumeric characters /^[0-9a-zA-Z]+$/.

I am having trouble forming regular expression containing all alphanumeric characters and one or two specific characters, such us _ or -.

This expression works for all alphanumeric characters /^[0-9a-zA-Z]+$/.

Share Improve this question asked Aug 29, 2012 at 10:09 krizajbkrizajb 1,8144 gold badges31 silver badges49 bronze badges 1
  • You need a regex to find another regex? – Gabber Commented Aug 29, 2012 at 10:34
Add a ment  | 

2 Answers 2

Reset to default 2

Try this:

/^[0-9a-zA-Z-_]+$/

If you enter the dash sign "-" at a position where it can be interpreted as a range such as _- it would mean any characters matching _ or above in the ascii table.

Add the special characters inside the square brackets

/^[0-9a-zA-Z_-]+$/

To use this regex in javascript use this code (yourPhrase is the string you check vs the regexp)

var rexp = /^[0-9a-zA-Z_-]+$/
if(rexp.test(yourPhrase)){
    //code to handle the test
}
发布评论

评论列表(0)

  1. 暂无评论