javascript - 将括号外的每个字符大写

标签 javascript regex

我有一个字符串,里面包含括号:

I want to uppercase this string (except this) and (this)

我想把它改成:

I WANT TO UPPERCASE THIS STRING (except this) AND (this)

在 JavaScript 中执行此操作的最佳方法是什么?如果我必须使用正则表达式,请清楚地解释,因为我不擅长正则表达式。

最诚挚的问候,

最佳答案

将批处理大写然后在括号之间小写可能更简单

var str = "I want to uppercase this string (except this) and (this)";

str = str.toUpperCase().replace(/(\(.+?)\)/g, m => m.toLowerCase());

为了保留大小写:

var str = "I want to uppercase this string (eXcEpT ThIs) and (ThIS)";

str = str.toUpperCase().replace(/\((.+?\))/g, (m, c, o) => str.substr(o, m.length));

> I WANT TO UPPERCASE THIS STRING (eXcEpT ThIs) AND (ThIS)

关于javascript - 将括号外的每个字符大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490738/

相关文章:

javascript - 文本框字符验证

javascript - 禁用按钮在 IE 10 中不起作用

javascript - 在 NodeJs 中,为什么新的 Date 构造函数将默认时间设置为上午 7 点?

php - 使用正则表达式匹配 Laravel 路由中的子字符串

c++ - C++ STL 中的正则表达式

c# - 使用 Regex .NET 样式的变量扩展

regex - 为什么 grep -n "[^aeiou]+"不返回不包含元音的行(当它们存在时)?

javascript - 无法理解为什么我的 JS 不起作用

php - 通过curl和PHP提交JavaScript点击

java - 在正则表达式 java 中获取括号内的文本以及分隔符?