查找下一个单词在JavaScript位置

问题描述 投票:0回答:1

我有一串这样的希伯来语单词:

כללהנההשמיםמספריםכבודאל。 הנהתחלה

以及位置在此字符串中为匹配索引的数组我需要在数组中某个位置之后找到单词的位置一个单词在字符串中可能会出现多次,因此我必须能够提供一个类似StartPosition的参数,而且我还需要不包括点,逗号和其他此类字符(以免扭曲结果索引)。这些原因使我无法使用代码this post

您能帮我吗?

谢谢!

javascript regex string indexof hebrew
1个回答
0
投票

您不希望使用的帖子代码非常正确。但是基本的正则表达式不是希伯来语专用的。因此,您必须自定义“无空格”,它是所需字符代码范围的/S。快速研究为您必须在正则表达式中使用的希伯来字符集带来了此结果:\u0590-\u05fe

编辑This Answer的代码为您带来了以下解决方案代码(请注意,您必须向后阅读希伯来语文本):

function getWordAt (str, pos) {

    // Perform type conversions.
    str = String(str);
    pos = Number(pos) >>> 0;

    // Search for the word's beginning and end.
    var left = str.slice(0, pos + 1).search(/[\u0590-\u05fe]+$/),
        right = str.slice(pos).search(/\s/);

    // The last word in the string is a special case.
    if (right < 0) {
        return str.slice(left);
    }

    // Return the word, using the located bounds to extract it from the string.
    return str.slice(left, right + pos);

}
© www.soinside.com 2019 - 2024. All rights reserved.