开发者

jquery - pattern matching

开发者 https://www.devze.com 2023-02-28 19:32 出处:网络
I开发者_运维问答\'ve been trying to use the jquery match() function, but I\'m not able to get anything I try to work...

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());
        });


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

Use a regular expression like below:

\(.+\)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消