轻松替换值

时间:2016-08-28 17:51:13

标签: javascript regex replace

编织:https://mikethedj4.github.io/kodeWeave/editor/#98f122f9b4f7b8f1ae0c945d3087f580

我正在使用下面的代码,但它已多次重复。

基本上我想转过来......

selected_text = editor.getSelection();  // Need to grab the Active Selection

editor.replaceSelection("<" + selected_text + ">");
editor.focus();

到此......

selected_text = editor.getSelection();  // Need to grab the Active Selection

editor.replaceSelection("<" + selected_text + ">").focus();

那里有转换器可以帮我吗?

我尝试使用....

preview.val( this.value.replace(/;\n            editor.focus();/g,".focus();") )

但它没有用,控制台也没告诉我任何事情:(

&#13;
&#13;
$(document).ready(function() {
  var editor  = $(".editor"),
      preview = $(".preview");
  
  // Remove new line and insert new line showing the text in value
  editor.keyup(function() {
    preview.val( this.value.replace(/;\n        htmlEditor.focus();/g,".focus();") )
  }).click(function() {
    this.select()
  })
  
  // Easily Select Converted Code
  preview.click(function() {
    this.select()
  })
  
    preview.val( editor.val().replace(/;\n        htmlEditor.focus();/g,".focus();") )
})
&#13;
body {
  margin: 0;
  background: #333;
}

.editor, .preview {
  position: absolute;
  width: 50vw;
  height: 100vh;
  padding: 1em;
  border: 0;
  border-radius: 0;
  resize: none;
}

.editor {
  left: 0;
  color: #0b0;
  background-color: #000;
}

.preview {
  right: 0;
  background-color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea class="editor" placeholder="Code with multiple lines here...">            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();</textarea>
<textarea class="preview" placeholder="Generated result here..."></textarea>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要转义括号:htmlEditor.focus\(\);。我还建议\s*代替\n一堆空格,但这取决于你。请确保使用正确数量的空格(12,而不是8,对于您给出的示例),或者可以使用\n *来确保必须有换行符,但允许任意数量的空格跟随它

请参阅下面的工作代码:

$(document).ready(function() {
  var editor  = $(".editor"),
      preview = $(".preview");
  
  // Remove new line and insert new line showing the text in value
  editor.keyup(function() {
    preview.val( this.value.replace(/;\s*htmlEditor.focus\(\);/g,".focus();") )
  }).click(function() {
    this.select()
  })
  
  // Easily Select Converted Code
  preview.click(function() {
    this.select()
  })
  
    preview.val( editor.val().replace(/;\s*htmlEditor.focus\(\);/g,".focus();") )
})
body {
  margin: 0;
  background: #333;
}

.editor, .preview {
  position: absolute;
  width: 50vw;
  height: 100vh;
  padding: 1em;
  border: 0;
  border-radius: 0;
  resize: none;
}

.editor {
  left: 0;
  color: #0b0;
  background-color: #000;
}

.preview {
  right: 0;
  background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea class="editor" placeholder="Code with multiple lines here...">            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();</textarea>
<textarea class="preview" placeholder="Generated result here..."></textarea>

答案 1 :(得分:1)

请注意()和。是需要在正则表达式中转义的特殊字符。 ()包含一个组和。代表任何角色。

我还使用\ s +来搜索多个空格

这是工作示例

&#13;
&#13;
var regex = /;\s+(htmlEditor\.focus\(\);)/g;
$(document).ready(function() {
  var editor  = $(".editor"),
      preview = $(".preview");
  
  // Remove new line and insert new line showing the text in value
  editor.keyup(function() {
    preview.val( this.value.replace(regex,".focus();\n") )
  }).click(function() {
    this.select()
  })
  
  // Easily Select Converted Code
  preview.click(function() {
    this.select()
  })
  
    preview.val( editor.val().replace(regex,".focus();\n") )
})
&#13;
body {
  margin: 0;
  background: #333;
}

.editor, .preview {
  position: absolute;
  width: 50vw;
  height: 100vh;
  padding: 1em;
  border: 0;
  border-radius: 0;
  resize: none;
}

.editor {
  left: 0;
  color: #0b0;
  background-color: #000;
}

.preview {
  right: 0;
  background-color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea class="editor" placeholder="Code with multiple lines here...">            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();            htmlEditor.replaceSelection("{" + selected_text + "}");
            htmlEditor.focus();</textarea>
<textarea class="preview" placeholder="Generated result here..."></textarea>
&#13;
&#13;
&#13;