Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

js 正则表达式 匹配

已知数据为___(1)___rcos(a+b)___(1)___测试转换___(1)___
求正则表达式转换为___(1)___rcos(a+b)___(2)___测试转换___(3)___

根据下划线和括号为开始结尾标识,其他包含括号的不需要转


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

// => 已知数据为___(1)___rcos(a+b)___(2)___测试转换___(3)___
demo('已知数据为___(1)___rcos(a+b)___(1)___测试转换___(1)___')

function demo (str) {
  let index = 0;
  return str.replace(/(_()(.*?)()_)/g, function (g, $1, $2, $3) {
    return $1 + ++index + $3
  })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...