大写括号外的每个字符

时间:2017-08-03 17:15:30

标签: javascript regex

我的字符串中包括括号:

  

我想大写这个字符串(除了这个)和(this)

我想将其转换为:

  

我想要超过这个字符串(除了这个)和(这个)

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

致以最诚挚的问候,

4 个答案:

答案 0 :(得分:2)

可能更简单的是大写字母然后是小写之间的小写

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)

答案 1 :(得分:1)

您可以使用正则表达式,然后使用回调替换



var str = "I want to uppercase this string (except this) and (this)";
var res = str.replace(/[^()](?=([^()]*\([^()]*\))*[^()]*$)/g, t => t.toUpperCase());

console.log(res)




正则表达式执行以下操作

  NODE                     EXPLANATION
-----------------------------------------
  [^()]                    any character except: '(', ')'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (                        group and capture to \1 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      [^()]*                   any character except: '(', ')' (0 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
      \(                       '('
--------------------------------------------------------------------------------
      [^()]*                   any character except: '(', ')' (0 or
                               more times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
      \)                       ')'
--------------------------------------------------------------------------------
    )*                       end of \1 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \1)
--------------------------------------------------------------------------------
    [^()]*                   any character except: '(', ')' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  /g                       global

没有任何正则表达式



var str = "I want to uppercase this string (except this) and (this)";
var par = false;
var res = str.split('').map(function(c) {
  par = c == '(' ? true : c == ')' ? false : par;
  return par ? c : c.toUpperCase();
}).join('')

console.log(res)




答案 2 :(得分:1)

这可能更简单:

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

var repl = str.replace(/[a-z]+(?![^()]*\))/g, m => m.toUpperCase())

console.log(repl);

//=> "I WANT TO UPPERCASE THIS STRING (except this) AND (this)"

这假设()在输入中均衡且未转义。

否定前瞻(?![^()]*\))断言我们匹配的字符串后面没有),中间没有()

答案 3 :(得分:1)

可能是一种更好的方法,但使用的方式却快速而肮脏



var x = 'I want to uppercase this string (except this) and (this)'.replace( /(^[^(]+)|(\)[^(]+)/g, function (a,b) { return (a || b).toUpperCase()})
console.log(x)