在 JavaScript 字符串数组中获取相邻字符的最佳方法是什么?
What's the best way to get adjacent characters in JavaScript array of strings?
假设我有这个:
var arr = [ "Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.", "The wizard quickly jinxed the gnomes before they vaporized.", "The quick brown fox jumps over the lazy dog."];
我试图找出一种方法,当我输入"jinxed"时,我也得到了"快速向导"和"之前侏儒"。不一定是整个单词,我只想在输入的旁边和之前获取几个字符。我一直在围绕这个碰壁,所以我感谢任何建议和提示。
谢谢!
function scan(arr, w) {
// This function is a first attempt to solve your specific problem.
// It just looks for the first occurence of w, the word you are looking for
// It does not cover edge or specific cases, neither is optimized.
// With more specifications, it can be generalized.
// Hope it can help.
var
// utils
regWord = /'w+/g,
wordsBefore, wordsAfter,
matchWord,
found = false;
// loop through your array
arr.forEach(function(item) {
if (found) {
// if we have already found the word, skip the iteration
return false;
}
// clear the arrays of words before and after
wordsBefore = [];
wordsAfter = [];
// strip the words from the array item
// every match is an iterable object containing the word, its index, and the item string
while (match = regWord.exec(item)) {
// get the matched word
matchWord = match[0].toLowerCase();
// if we have found the word we are looking for
if (matchWord === w.toLowerCase()) {
// flag it
found = true;
} else {
// it is not a match, but a word that could be returned in the array before or after
if (!found) {
// if we have not found the word yet
// populate the array of words before
wordsBefore.push(matchWord);
} else {
// otherwise, if we are here, it means that we are in a sentence containing
// the word we are looking for, and we have already found it
// populate the array of words after
wordsAfter.push(matchWord);
}
}
}
});
// return an object containing
return {
// the words before
wordsBefore: wordsBefore,
// the words after
wordsAfter: wordsAfter
}
}
使用
split
获取 jinxed
之前和之后的内容,然后使用 substr
获取每个部分中的最后一个字符或第一个字符:
var a = "The wizard quickly jinxed the gnomes before they vaporized";
var s = a.split('jinxed');
s == [ 'The wizard quickly ',' the gnomes before they vaporized' ]
s[0].substr(-10,10) == 'd quickly '
s[1].substr(0,10) == ' the gnome'
您可以使用正则表达式匹配:
function search(s,q, msg) {
// match the characters before and after
var m = s.match(new RegExp("(.|^)" + q + "(.|$)"));
console.log(m);
// if there was a match at all, then drop the 1st (which is the whole match: " jinxed ")
if (m) {
if(m.length>1) {m = m.splice(1)}
// m[0] in the character before, m[1] is the one after
alert(msg + " '" + m[0] + "', '" + m[1] + "'");
} else {
alert(msg + " not found");
}
}
search("The wizard quickly jinxed the gnomes before they vaporized.","jinxed", "in the middle");
search("jinxed the gnomes before they vaporized.","jinxed", "in the beginning");
search("The wizard quickly jinxed","jinxed","in the end");
search("jinxed","jinxed","full");
search("The wizard quickly","jinxed","not in string");
请注意,当您正在搜索的字符串位于行的开头或结尾时,这也有效。
相关文章:
- 在JavaScript中输出转义字符
- 不同浏览器中的空白字符正则表达式行为
- Regex匹配除“”之外的所有字符;.js”;
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 使用RegExp查找url中的字符
- 为react组件传递道具的最佳方式
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 让Webpack管理Quirky AMD定义的最佳方式
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 检查另一个字符串 JavaScript 中一个字符串是否有任何字符的最佳方法
- 在 url 中编码特殊字符的最佳方法
- 在 JavaScript 字符串数组中获取相邻字符的最佳方法是什么?
- 屏蔽文本框控件的最佳方式是使用javascript允许字符串值在一对3个字符中
- 为每个已知的字符数添加新的行字符的最佳方式
- 在Javascript中检查一个字符是否为任何书面语言中的元音的最佳方法
- 检查一个字符是否是javascript中的字母数字的最佳方法
- 计算推特的Javascript字符数的最佳方式