regular expression for ends with some word
I want 开发者_如何学Goto build regular expression for series
cd1_inputchk,rd_inputchk,optinputchk where inputchk is common (ending characters)
please guide for the same
Very simply, it's:
/inputchk$/
On a per-word basis (only testing matching /inputchk$/.test(word) ? 'matches' : 'doesn\'t match';
). The reason this works, is it matches "inputchk"
that comes at the end of a string (hence the $
)
As for a list of words, it starts becoming more complicated.
- Are there spaces in the list?
- Are they needed?
I'm going to assume no
is the answer to both questions, and also assume that the list is comma-separated.
There are then a couple of ways you could proceed. You could use list.split()
to get an array of each word, and teast each to see if they end in inputchk
, or you could use a modified regular expression:
/[^,]*inputchk(?:,|$)/g
This one's much more complicated.
[^,]
says to match non-,
characters*
then says to match 0 or more of those non-,
chars. (it will be greedy)inputchk
matchesinputchk
(?:...)
is a non-capturing parenthesis. It says to match the characters, but not store the match as part of the result.,
matches the,
character|
says match one side or the other$
says to match the end of the string
Hopefully all of this together will select the strings that you're looking for, but it's very easy to make a mistake, so I'd suggest doing some rigorous testing to make sure there aren't any edge-conditions that are being missed.
This one should work (dollar sign basically means "end of string"):
/inputchk$/
精彩评论