【发布时间】:2022-01-02 04:38:46
【问题描述】:
我有一个用户可以使用 Ctrl+V 粘贴的文本框。我想限制文本框只接受 GUID。我试图编写一个小函数,将输入字符串格式化为基于 RegEx 的 GUID,但我似乎无法做到。我尝试按照以下帖子进行操作: Javascript string to Guid
function stringToGUID()
{
var strInput = 'b6b954d9cbac4b18b0d5a0f725695f1ca98d64e456f76';
var strOutput = strInput.replace(/([0-f]{8})([0-f]{4})([0-f]{4})([0-f]{4})([0-f]{12})/,"$1-$2-$3-$4-$5");
console.log(strOutput );
//from my understanding, the input string could be any sequence of 0-9 or a-f of any length and a valid giud patterened string would be the result in the above code. This doesn't seem to be the case;
//I would like to extract first 32 characters; how do I do that?
}
【问题讨论】:
-
"我好像做不到" - 为什么?你遇到了什么问题?
-
据我了解,我使用的正则表达式模式不仅会找到一个 guid,而且只会提取前 32 个字母数字。好像不是这样的。
-
用户能否粘贴已包含连字符的 GUID?
-
十六进制字符需要
[0-9a-fA-F]。 -
@SoftwareDveloper 在这种情况下,您需要在进一步处理字符串之前删除连字符。
标签: javascript regex string