我试图使用.replace()替换特定的字符串。第一次和最后一次搜索被替换但不是第二次

时间:2017-09-25 07:46:41

标签: javascript regex

我正在尝试使用.replace()替换特定的字符串。第一次和最后一次搜索正在被替换,但不是第二次。



private void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}



public void setFullscreen(boolean fullscreen) {

    WindowManager.LayoutParams attrs = getWindow().getAttributes();
    if (fullscreen) {
        attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        attrs.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        attrs.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        attrs.flags |= WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;

    }
    else {
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    }
    getWindow().setAttributes(attrs);
    hideSystemUI();
}




1 个答案:

答案 0 :(得分:0)

问题是:如何用img标签替换带有捕获的url的文本作为src。我们需要在这里捕捉一些群体:

我已经更改了正则表达式,因此捕获了网址:

!\[[^\]]*\]      # match the ![alt text]. No need to capture this one.
\(
 (               # group 1 start
 [^)]*           # to capture the link
 )               # group 1 end
\)

我们要找的是用img src=<group 1>替换完整的匹配。现在看一下完整的片段:

var text = "![alt text](https://www.reduceimages.com/img/image-after3.jpg) adadw  hjagwdjh ![alt text](https://www.reduceimages.com/img/image-after2.jpg)  akjhwdhawk ![alt text](https://www.reduceimages.com/img/image-afte1r.jpg)";

console.log("FInal Result --- > " + formatText(text));

function toImg(str, group1) {
   return "<img src=\'" + group1 + "'>";
}

function formatText(content) {
  const regex = /!\[[^\]]*\]\(([^)]*)\)/g;
  return content.replace(regex, toImg);
}