如何删除文本中的特定符号

问题描述 投票:0回答:1

我正在尝试从传入值中删除特定文本

changeable:# this can be changeable
global:# this is global
manager:# this is manager 

我想要的是:

this can be changeable
this is global
this is manager 

如何将#后的空格也删除

javascript
1个回答
0
投票

您可以使用替换方法

  • [^-字符串开头
  • [[^#]*-匹配#零个或多个时间以外的任何内容
  • [#-匹配#
  • [\s*-匹配空格字符零个或多个时间

let strArr = [`changeable:# this can be changeable`, `global:# this is global`, `manager:# this is manager`]

let textModifier = (str) => {
  console.log(str.replace(/^[^#]*#\s*/, ''))
}

strArr.forEach(textModifier)
© www.soinside.com 2019 - 2024. All rights reserved.