JavaScript 正则表达式 - 替换时强制区分大小写

发布于 2024-08-14 03:57:26 字数 191 浏览 3 评论 0原文

JavaScript 正则表达式标准是否支持在搜索/替换时强制区分大小写?

我通常知道 \u 等选项用于在正则表达式的替换部分中强制捕获组的大小写。但我无法弄清楚如何在 JavaScript 正则表达式中执行此操作。 我无权访问 JavaScript 代码本身,但我正在使用的程序允许输入正则表达式字符串,并将其传递给正则表达式引擎本身。

Does the JavaScript regex standard support forcing case when doing a search/replace?

I am generally aware of the \u etc. options for forcing the case of a capture group in the replacement portion of a regular expression. I am unable to figure out how to do this in a JavaScript regex though. I do not have access to the JavaScript code itself but rather a program I am using allows entry of the regex strings which it passes through to the regex engine itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不美如何 2024-08-21 03:57:26

如果我理解正确的话,不会。

您无法在正则表达式模式中评估 javascript(将匹配项转换为大写)。

我只知道 str.replace() 转换为大写。

If I understand you correctly, no.

You cannot evaluate javascript (to transform matches to uppercase) from within a regex pattern.

I'm only aware of str.replace() to convert to uppercase.

日裸衫吸 2024-08-21 03:57:26

字符串匹配是使用正则表达式完成的。但是,字符串替换只关心您要替换的主题字符串的哪一部分(由正则表达式匹配提供),并且它只是根据您提供的内容进行直接字符串替换:

var subject = "This is a subject string";

// fails, case-sensitive match by default
subject.replace(/this/, "That"); 

// succeeds, case-insensitive expression using /i modifier, replaces the word "This" with the word "That"
subject.replace(/this/i, "That"); 

现在,如果您想捕获匹配字符串的一部分并使用它来更改大小写,您也可以使用表达式组(表达式中的括号)来做到这一点:

var subject = "This is a subject string";
var matches = subject.match(/(subject) string/i);
if (matches.length > 0)
{
    // matches[0] is the entire match, or "subject string"
    // matches[1] is the first group match, or "subject"
    subject.replace(matches[1], matches[1].toUpperCase());
    // subject now reads "This is a SUBJECT string"
}

简而言之,进行匹配可以处理区分大小写的情况如果你愿意的话。进行替换就像告诉它使用什么直接字符串进行替换一样简单。

String Matching is done using regular expressions.String Replacement, however, only cares about what portion of the subject string you are replacing (which it is fed by the regular expression match), and it just does direct string replacement based on what you give it:

var subject = "This is a subject string";

// fails, case-sensitive match by default
subject.replace(/this/, "That"); 

// succeeds, case-insensitive expression using /i modifier, replaces the word "This" with the word "That"
subject.replace(/this/i, "That"); 

Now, if you wanted to capture a part of the matched string and use it to change the case, you can do that as well using expression groups (parentheses in your expression):

var subject = "This is a subject string";
var matches = subject.match(/(subject) string/i);
if (matches.length > 0)
{
    // matches[0] is the entire match, or "subject string"
    // matches[1] is the first group match, or "subject"
    subject.replace(matches[1], matches[1].toUpperCase());
    // subject now reads "This is a SUBJECT string"
}

In short, doing a match you can handle case-sensitivity if you wish. Doing a replacement is as simple as telling it what direct string to use for replacement.

狼性发作 2024-08-21 03:57:26

JavaScript 正则表达式默认区分大小写,如果传递 /i 标志,则支持不区分大小写。

JavaScript regular expressions are case sensitive by default and support case insensitivity if passed the /i flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文