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

标签 regex coldfusion cfml lucee

我正在尝试在 Lucee 中编写一个正则表达式来模拟前端的 JS。由于 Lucee 的正则表达式似乎不支持 unicode,我该怎么做。

这是JS

function charTest(k){
    var regexp = /^[\u00C0-\u00ff\s -\~]+$/;
    return regexp.test(k)
}

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

这是我在 Lucee 中尝试过的
regexp = '[\u00C0-\u00ff\s -\~]+/';
writeDump(reFind(regexp,"测));
writeDump(reFind(regexp,"test));

我也试过
 regexp = "[\\p{L}]";

但转储总是 0

最佳答案

编辑:给我一秒钟。我想我错误地解释了你最初的 JS 正则表达式。修复它。

编辑 2:时间超过一秒钟。你原来的 JS 正则表达式是:"/^[\u00C0-\u00ff\s -\~]+$/" .这是:

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.

我错过了可打印拉丁语基本字符集的后半部分。我已经更新了我的正则表达式和测试以包含它。有一些方法可以简写其中一些标识符,但我希望它是明确的。

你可以试试这个:
<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

正则表达式所做的就是查看您的输入字符串,如果没有在 chr(192) 之间找到值和 chr(255) ,它将返回您选择的字符串,否则它将不返回任何内容。

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

您是否需要提醒此功能,例如 Javascript?如果需要,您可以只输出 1 或 0 来确定此函数是否确实找到了您要查找的字符。

关于regex - 如何在 Lucee 中模拟 Unicode JS 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52560727/

相关文章:

c++ - 在 CentOS 6.4 上使用 Boost.Regex 1.53 和 gcc 4.8.1 随机断言失败

ColdFusion CFFILE 限制文本文件上传

javascript - 通过ajax发送base64图像数据到cfc

ruby - 将字符串拆分为 Ruby 中的一对字符

php - 正则表达式匹配某些数字组合模式

java - 如何使用 EWS 托管 API 和 ColdFusion 搜索收件箱?

node.js - 如何将 padbytes 函数转换为 coldfusion

coldfusion - ColdFusion 中数组的奇怪行为

coldfusion - 使用动态参数数组调用函数

regex - 有什么作用? : do in regex