开发者

Using jQuery to find keywords in string

开发者 https://www.devze.com 2022-12-22 17:54 出处:网络
In php, I have used preg_match_all() to find keywords (using the format %keyword%) in a string, which works wonderfully and returns an array of all found keywords.

In php, I have used preg_match_all() to find keywords (using the format %keyword%) in a string, which works wonderfully and returns an array of all found keywords.

What I would like to do, is do the same thing using jQuery or javascript. I have tried using the filter() function, and it kind of works. But I think I am missing something.

How can I find ALL instances of the keyword in a string, using a regex?

Here is what I have worked out so far:

$("[id^='editableContent-']").filter(function() {
   开发者_运维问答 alert($(this).html().match(/\%(.*?)\%/));
});

Any suggestions are welcome!

EDIT:

For anyone that reads this, I have figured out how to get my keywords into an array:

$("[id^='editableContent-']").filter(function() {
    var keys = $(this).html().match(/\%(.*?)\%/gi).toString();
    var keys_arr = keys.split(',');
    alert(keys_arr[1]);
});


For anyone that reads this, I have figured out how to get my keywords into an array:

$("[id^='editableContent-']").filter(function() {
    var keys = $(this).html().match(/\%(.*?)\%/gi).toString();
    var keys_arr = keys.split(',');
    alert(keys_arr[1]);
});
0

精彩评论

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