【问题标题】:How can I replace the text between two delimiters with a blank? [closed]如何用空格替换两个分隔符之间的文本? [关闭]
【发布时间】:2020-02-06 11:42:29
【问题描述】:

我有一条消息,通过输入字段接收。例如:

This is *red* colour.

我想用空行(“___”)替换两个星号之间的所有内容,因此结果将是:

This is ___ colour.

我怎样才能做到这一点?

【问题讨论】:

  • 单行是message.replace(/\*.*?\*/g, '___');

标签: javascript regex angular typescript


【解决方案1】:
function myFunction() {
  var str = "This is *red* color";
  var startIndex = nthIndex(str,'*',1);
  var endIndex = nthIndex(str,'*',2);
  var output = str.replace(str.substring(startIndex, (endIndex+1)), "_");
}

function nthIndex(str, pat, n){
    var L= str.length, i= -1;
    while(n-- && i++<L){
        i= str.indexOf(pat, i);
        if (i < 0) break;
    }
    return i;
}

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2011-12-22
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多