最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - JQuery Regular expression to accept alphanumeric characters and ' , - - Stack Overflow

matteradmin3PV0评论

I'm trying to figure out how to get my regular expression to accept certain special characters: ', , and - along with alphanumeric characters. I've had a stab at it but to no avail, and I'm quite new to regex, can anyone help?

Here was my attempt which, surprisingly, didn't work...

/^\d+/,\'\-\$/i

I'm trying to figure out how to get my regular expression to accept certain special characters: ', , and - along with alphanumeric characters. I've had a stab at it but to no avail, and I'm quite new to regex, can anyone help?

Here was my attempt which, surprisingly, didn't work...

/^\d+/,\'\-\$/i
Share Improve this question edited Feb 21, 2011 at 14:26 sth 230k56 gold badges287 silver badges370 bronze badges asked Feb 21, 2011 at 13:52 pampam 1431 gold badge3 silver badges9 bronze badges 1
  • 1 This is just JavaScript, not jQuery. (In truth, it could be construed as language-agnostic, to some extent.) – Reid Commented Feb 21, 2011 at 13:58
Add a ment  | 

3 Answers 3

Reset to default 7

Something like this?

/[0-9a-zA-Z',-]+/

if it has to be a full string, you can use

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

Try

/^[\w',-]*$/

(assuming you mean ASCII letters, digits and underscore by "alphanumeric").

\d is shorthand for [0-9], which is not any alphanumeric character.

/^[\w,'-]+$/i

should do the trick.

What this is saying:

^ - match the start of the line
[ - match any of the following characters (group #1)
    \w - any word (meaning differs depending on locale;
         generally, any letter, number or the `-` character.)
    ,  - a ma
    '  - an apostrophe
    -  - a dash
] - end group #1
+ - one or more times
$ - match the end of the line
/i - set case-insensitivity.
Post a comment

comment list (0)

  1. No comments so far