0

I've been trying to use the jquery match() function, but I'm not able to get anything I try to work...

Basically, I'm trying to match any string between parentheses:

(sample) (pokemon) (su54gar99)

... etc.

Tried this (the latest attempt... not sure how many I've gone through at this point...)

$('.acf').each(function() {
                var ac = $(this);
                ac.data('changed', false);
                ac.data('alias', 'ac-real-');
                ac.data('rx',  /\s*\(\s+\)\s*/i);
                ac.data('name', ac.attr('name'));
                ac.data('id', ac.attr('id'));
                ac.data('value', ac.val());
        });

2 Answers 2

2

Why are you using \s ? It matches any whitespace character

Use a regular expression like below:

\(.+\)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I figured it out like 1 minutes after I posted... Thank you, though!
Never use the dot when there is a more precise expression (in this case: [^()]). The expression: \(.+\) fails if there are multiple sets of parentheses. Note that the lazy dot version: \(.+?\) also fails if the parentheses are nested. Use: \([^()]*\) to safely match only innermost sets of parentheses.
It was just a suggestion to correct what was wrong. Anyway, thanks for your input.
1

I suggest your fault is, that "\s" only stands for whitespace characters, but not for normal characters ;).

Second, for finding EACH and not stopping after 1 result, add the "g" (for global) modifier.

Try something like this:

var text = "(sample) (pokemon) (su54gar99)";
var found = text.match(/\s*\(([a-z,0-9]*)\)/ig);
$.each(found, function(i, v) {
    alert(i+" = "+v);
});

Works with at least the given example ;).

TIP: Visit: http://www.w3schools.com/jsref/jsref_obj_regexp.asp Best side for learning RegEx, in my opinion :P.

1 Comment

Curses, beat me. /^\(.|\d*?\)$/ <- any downfalls?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.