XRegExp匹配希腊语的整个单词

时间:2014-05-22 22:51:26

标签: javascript unicode xregexp

<html>
<head>
        <title>Test</title>
        <meta charset="UTF-8">

<script src="xregexp.js"></script>
<script src="unicode-base.js"></script>
<script src="unicode-scripts.js"></script>
<script>
var wordsToMatch = "Gr|Greek|ΕΛΛΗΝΙΚΑ";

var regexPattern = '\\b('+wordsToMatch+')';

var referenceRegex = new XRegExp(regexPattern, 'mi');
//var testString = "ΕΛΛΗΝΙΚΑ"; //null  -  ???
var testString = "Gr"; //Gr,Gr  -  OK AS IT SHOULD
var match = referenceRegex.exec(testString); 

function myFunction()
{
    alert(match);
}
</script>

</head>
<body onload="myFunction()">


</body>
</html>

我相信我的例子是可以解释的,它匹配精美的英语单词,我也希望也匹配希腊语单词。我不熟悉xregexp的unicode附加组件,我的模式实现了与regexp一起使用。

2 个答案:

答案 0 :(得分:0)

除了unicode base之外,您还需要脚本插件:

<script src="xregexp.js"></script>
<script src="addons/unicode/unicode-base.js"></script>
<script src="addons/unicode/unicode-scripts.js"></script>

<script>
  XRegExp("\\p{Greek}+").test("ΕΛΛΗΝΙΚΑ"); // true
</script>

答案 1 :(得分:0)

<html>
<head>
        <title>Test</title>
        <meta charset="UTF-8">
<script>
var wordsToMatch = "Gr|Greek|ΕΛΛΗΝΙΚΑ";

var regexPattern = '('+wordsToMatch+')';

var referenceRegex = new RegExp(regexPattern, 'mi');
//var testString = "ΕΛΛΗΝΙΚΑ"; //Works now fine
var testString = "Gr"; //Gr,Gr  -  OK AS IT SHOULD
var match = referenceRegex.exec(testString); 

function myFunction()
{
    alert(match);
}
</script>

</head>
<body onload="myFunction()">


</body>
</html>

以上工作正常。正则表达式和测试中的一点点阅读清除了我的问题。删除了\ b而且XRegExp也不需要...