Javascript replace() with case-change

后端 未结 2 560
别那么骄傲
别那么骄傲 2020-12-19 09:20

Is there an easy way to change the case of a matched string with javascript?

Example

String :

  • something
  • <
    2条回答
    •  情歌与酒
      2020-12-19 10:16

      easiest is probably to use a regex match and then use .toUpperCase on the match. I modified the regex slightly to add in a second capture group

      var str = '
    • something
    • '; var arr = /<([\w]+)([^>]*>.*?<\/)\1>/.exec(str); str = '<' + arr[1].toUpperCase() + arr[2] + arr[1].toUpperCase() + '>';

    提交回复
    热议问题