首先,我不知道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 loopEDIT: 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 loopTwo 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.
更多推荐
发布评论