首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Lucee中模拟Unicode JS正则表达式

如何在Lucee中模拟Unicode JS正则表达式
EN

Stack Overflow用户
提问于 2018-09-28 18:12:31
回答 1查看 100关注 0票数 4

我试着用Lucee写一个常规的快车,以模仿前端的JS。因为Lucee的regex似乎不支持unicode,我该怎么做。

这是JS

代码语言:javascript
复制
function charTest(k){
    var regexp = /^[\u00C0-\u00ff\s -\~]+$/;
    return regexp.test(k)
}

if(!charTest(thisKey)){
    alert("Please Use Latin Characters Only");
    return false;
}

这就是我在露茜身上尝试过的

代码语言:javascript
复制
regexp = '[\u00C0-\u00ff\s -\~]+/';
writeDump(reFind(regexp,"测));
writeDump(reFind(regexp,"test));

我也试过

代码语言:javascript
复制
 regexp = "[\\p{L}]";

但是转储总是0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-28 20:46:08

编辑:给我一秒钟时间。我想我不正确地解释了你最初的JS正则表达式。修好它。

编辑2: --超过一秒钟。您最初的JS regex是:"/^[\u00C0-\u00ff\s -\~]+$/"。这是:

代码语言:javascript
复制
Basic parts of regex:
"/..../" == signifies the start and stop of the Regex.
"^[...]" == signifies anything that is NOT in this group
"+" == signifies at least one of the previous
"$" == signifies the end of the string

Identifiers in the regex:
"\u00c0-\u00ff" == Unicode character range of Character 192 (À) 
                   to Character 255 (ÿ). This is the Latin 1 
                   Extension of the Unicode character set.
"\s" == signifies a Space Character
" -\~" == signifies another identifier for a space character to the 
          (escaped) tilde character (~). This is ASCII 32-126, which
          includes the printable characters of ASCII (except the DEL
          character (127). This includes alpha-numerics amd most punctuation.

我错过了你可打印的拉丁文基本字符集的下半部分。我已经更新了我的正则表达式和测试以包括它。有一些方法可以缩短这些标识符,但我希望它是明确的。

你可以试试这个:

代码语言:javascript
复制
<cfscript>
//http://www.asciitable.com/
//https://en.wikipedia.org/wiki/List_of_Unicode_characters
//https://en.wikipedia.org/wiki/Latin_script_in_Unicode


function charTest(k) {
  return 
    REfind("[^" 
      & chr(32) & "-" & chr(126) 
      & chr(192) & "-" & chr(255) 
      & "]",arguments.k) 
    ? "Please Use Latin Characters Only" 
    : "" 
  ;
}


// TESTS
writeDump(charTest("测")); // Not Latin
writeDump(charTest("test")); // All characters between 31 & 126
writeDump(charTest("À")); // Character 192 (in range)
writeDump(charTest("À ")); // Character 192 and Space
writeDump(charTest("     ")); // Space Characters
writeDump(charTest("12345")); // Digits ( character 48-57 )
writeDump(charTest("ð")); // Character 240 (in range) 
writeDump(charTest("ℿ")); // Character 8511 (outside range)
writeDump(charTest(chr(199))); // CF Character (in range)
writeDump(charTest(chr(10))); // CF Line Feed Character (outside range)
writeDump(charTest(chr(1000))); // CF Character (outside range)

writeDump(charTest("
")); // CRLF (outside range)

writeDump(charTest(URLDecode("%00", "utf-8"))); // CF Null character (outside range)

//writeDump(asc("测"));
//writeDump(asc("test"));
//writeDump(asc("À"));
//writeDump(asc("ð"));
//writeDump(asc("ℿ"));
</cfscript>

https://trycf.com/gist/05d27baaed2b8fc269f90c7c80a1aa82/lucee5?theme=monokai

regex所做的就是查看您的输入字符串,如果它在chr(192)chr(255)之间找不到一个值,它将返回您选择的字符串,否则它将什么也不返回。

我认为您可以直接访问255以下的UNICODE字符。我得测试一下。

您需要像Javascript那样提醒这个函数吗?如果需要,只需输出1或0就可以确定该函数是否确实找到了要查找的字符。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52560727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档