用于搜索数组的JavaScript(jQuery)Regular Expression(JavaScript (jQuery) Regular Expression for searching thr

编程入门 行业动态 更新时间:2024-10-24 18:21:03
用于搜索数组的JavaScript(jQuery)Regular Expression(JavaScript (jQuery) Regular Expression for searching through an array)

首先,我不知道RegEx,但我正在尝试将某些东西拼凑在一起以使其发挥作用。 只是想让你被预先警告。 ;)

无论如何,我正在尝试创建一个正则表达式来从数组中获取一个单词,看看它是否与另一个数组中的单词匹配。 如果关键字数组字符串包含searchTerm字,我只希望搜索返回true。 (即一个人会是假的,所以会是假的)。 任何帮助是极大的赞赏。

var searchTerm = ['one','two','three']; var keywords = ['String which contains one', 'This string is 2', 'Three is here']; var keywordIndex; // loop through each keyword array $.each(keywords, function(i) { var found = false; $.each(searchTerm, function(j) { var rSearchTerm = new RegExp('\b' + searchTerm[j] + '\b',i); // if search term is found, swap accordion div content if (keywords[i].search(rSearchTerm) > -1) { found = true; keywordIndex = i; // grouping keyword is in return false; } }); // end searchTerm loop if (found) { return false; } }); // end keyword loop

编辑:在此示例中,只有关键字[1]会失败。

First and foremost, I do not know RegEx but am trying to piece something together to make this work. Just wanted you to be forewarned. ;)

Anyways, I'm trying to create a regular expression to take a word from an array and see if it matches a word in another array. I only want the search to return true if the keyword array string contains the searchTerm word. (i.e. oneone would be false, so would ones). Any help is GREATLY appreciated.

var searchTerm = ['one','two','three']; var keywords = ['String which contains one', 'This string is 2', 'Three is here']; var keywordIndex; // loop through each keyword array $.each(keywords, function(i) { var found = false; $.each(searchTerm, function(j) { var rSearchTerm = new RegExp('\b' + searchTerm[j] + '\b',i); // if search term is found, swap accordion div content if (keywords[i].search(rSearchTerm) > -1) { found = true; keywordIndex = i; // grouping keyword is in return false; } }); // end searchTerm loop if (found) { return false; } }); // end keyword loop

EDIT: In this example, only keywords[1] would fail.

最满意答案

这将有效:

var searchTerm = ['one','two','three']; var keywords = ['String which contains one', 'This string is 2', 'Three is here']; var keywordIndex; // loop through each keyword array $.each(keywords, function(i) { $.each(searchTerm, function(j) { var rSearchTerm = new RegExp('\\b' + searchTerm[j] + '\\b','i'); // if search term is found, swap accordion div content if (keywords[i].match(rSearchTerm)) { keywordIndex = i; // grouping keyword is in alert(keywords[i]); //debug } }); // end searchTerm loop }); // end keyword loop

两个更正:

标志应该是字符串"i" ,而不是i ,这是一个本地int变量。 反斜杠需要转义,因为它是字符串的一部分(字面反斜杠): "\\b" , '\b'作为垃圾字符串出现:``。

一些注意事项:我已将search更改为match (我从未使用过搜索,所以我想确保它有效)。 次优化 - 如果更改循环的嵌套(例如, $.each(searchTerm first),则可以在外循环中创建正则表达式)。

This will work:

var searchTerm = ['one','two','three']; var keywords = ['String which contains one', 'This string is 2', 'Three is here']; var keywordIndex; // loop through each keyword array $.each(keywords, function(i) { $.each(searchTerm, function(j) { var rSearchTerm = new RegExp('\\b' + searchTerm[j] + '\\b','i'); // if search term is found, swap accordion div content if (keywords[i].match(rSearchTerm)) { keywordIndex = i; // grouping keyword is in alert(keywords[i]); //debug } }); // end searchTerm loop }); // end keyword loop

Two corrections:

the flag should be a string "i", not i, which is a local int variable. Backslash needs escaping, as it is part of a string (literal backslash): "\\b", '\b' comes out as a garbage string: ``.

Some notes: I've changes search to match (I never used search, so I wanted to make sure it works). Minor optimization - If you change the nesting of the loops (eg, $.each(searchTerm first), you can create the regex in the outer loop.

更多推荐

本文发布于:2023-08-05 14:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1433965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   jQuery   JavaScript   Regular   searching

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!