Gerald Ferreira
Gerald Ferreira

Reputation: 1337

Regex pattern match small issue with brackets in pattern match

Someone helped me earlier with this regex:

checkxls = checkxls.match(/'[^']*'(?:, '[^']*'){13};/g);

The purpose is to capture a exact patter like this

'', '', '', '', '', '', '', '', '', '', '', '', '', '';

Now I want to do the same thing but just with a pattern like this

('.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?'),

I am not sure how to modify the regex to capture the expression like above

checkxls = checkxls.match(/\('[^']*'(?:, '[^']*'){13}\),/g);

i have tried it like above inserting \( and \) but it does not seem to help me...

I want to match only the exact pattern like this

('.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?', '.*?'),

and if it is like this

('sometext', '0', '', ''), << I want to get removed

Upvotes: 2

Views: 261

Answers (1)

quantumSoup
quantumSoup

Reputation: 28172

Just change quantifier from * to +:

checkxls = checkxls.match(/'[^']+'(?:, '[^']+'){13};/g);

On the pattern(s) (coutersy of polygenelubricants)

The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.

The * repetition specifier can be used to match "zero-or-more times" of the preceding pattern.

The + repetition specifier can be used to match "one-or-more times" of the preceding pattern.

The (?:…) is a positive lookahead; it can be used to assert that a certain pattern DOES match, looking ahead (i.e. to the right) of the current position.

The {n} is the finite repetition specifier which means "match the preceding pattern n times."

The /g modifier at the end is used to perform a global match (find all matches rather than stopping after the first match).

References

Upvotes: 1

Related Questions