英文:
Regex for new line not working in textarea jQuery
问题
I am trying to convert returned output copied from router that consists of multiple \r\n
to new lines to create a neatly formatted result configuration with indentation for user viewing from the UI. However, my script below is not working. I have referred to some references from Googling, but it hasn't been very helpful. Does anyone have a solution? Currently, I am developing using the MacOS platform and the latest Chrome.
Example output:
{
"status": "success",
"output": {
"ipv4_address": "xxx/xx",
},
"ne_response": "... \r\r\n--- {master}\r\nRouter@xyz> show ... inet address xxx/30\r\n\r\n{master}\r\nRouter@xyz> "
}
Current output (\r\n
not working):
HTML:
<textarea class="form-control" id="viewConfig"></textarea>
jQuery (none of these convert to new lines):
$("#viewConfig").on("keyup", function(){
var vConfig = $("#viewConfig").val().replace(/\r\n|\r|\n/g, "\\n"); // double backslash not working
var vConfig = $("#viewConfig").val().replace(/\r\n|\r|\n/g, "\n"); // single backslash not working
$('#viewConfig').html(vConfig);
});
$("#viewConfig").on("keyup", function(){
var vConfig = $('#viewConfig').val().replace(/\n/g, "\\n"); // double backslash not working
var vConfig = $('#viewConfig').val().replace(/\n/g, "\n"); // single backslash not working
$('#viewConfig').html(vConfig);
});
$("#viewConfig").on("keyup", function(){
var vConfig = $("#viewConfig").val().replace(/[\r\n]+/gm, " ");
$('#viewConfig').html(vConfig);
});
英文:
I am trying to convert returned output copied from router that consist of multiple \r\n
to new line to be as pretty result configuration with indention for user viewing from UI. However my script below is not working. I have refer some reference from googling also not help much. Anyone have solution? Currently I am develop using MacOS platform and latest Chrome.
example output
{
"status": "success",
"output": {
"ipv4_address": "xxx/xx",
},
"ne_response": "... \r\r\n--- {master}\r\nRouter@xyz> show ... inet address xxx/30\r\n\r\n{master}\r\nRouter@xyz> "
}
current output (\r\n
not working)
html
<textarea class="form-control" id="viewConfig"></textarea>
jQuery (all not convert to new line)
$("#viewConfig").on("keyup", function(){
var vConfig = $("#viewConfig").val().replace(/\r\n|\r|\n/g, "\\n"); // double backslash not working
var vConfig = $("#viewConfig").val().replace(/\r\n|\r|\n/g, "\n"); // single backslash not working
$('#viewConfig').html(vConfig);
});
$("#viewConfig").on("keyup", function(){
var vConfig = $('#viewConfig').val().replace(/\n/g, "\\n"); // double backslash not working
var vConfig = $('#viewConfig').val().replace(/\n/g, "\n"); // single backslash not working
$('#viewConfig').html(vConfig);
});
$("#viewConfig").on("keyup", function(){
var vConfig = $("#viewConfig").val().replace(/[\r\n]+/gm, "&#13;&#10;");
$('#viewConfig').html(vConfig);
});
答案1
得分: 1
IIUC,您实际要做的是将多次出现的\r
或\n
替换为单个\n
。您可以使用正则表达式[\r\n]+
来匹配一个或多个\r
或\n
字符。然后,您可以用单个换行符"\n"
来替换它。请注意,当引用文本区域时,您需要替换的是val
而不是html
。
在文本框中输入一个字符以查看效果:
$("#viewConfig").val("... \r\r\n--- {master}\r\nRouter@xyz> show ... inet address xxx/30\r\n\r\n{master}\r\nRouter@xyz> ")
$("#viewConfig").on("keyup", function(){
var vConfig = $(this).val().replace(/[\r\n]+/g, "\n");
$(this).val(vConfig);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="form-control" id="viewConfig" rows="10" cols="30"></textarea>
请注意,您可以在事件处理程序中使用$(this)
来引用$("#viewConfig")
(参见手册)。
英文:
IIUC, what you actually want to do is replace multiple occurrences of \r
or \n
with a single \n
. You can do that with the regex [\r\n]+
, which will match one or more \r
or \n
characters. You can then replace that with a single newline "\n"
. Note that you need to replace the val
, not the html
when referring to a textarea.
Type a character in the textbox below to see the effect:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#viewConfig").val("... \r\r\n--- {master}\r\nRouter@xyz> show ... inet address xxx/30\r\n\r\n{master}\r\nRouter@xyz> ")
$("#viewConfig").on("keyup", function(){
var vConfig = $(this).val().replace(/[\r\n]+/g, "\n");
$(this).val(vConfig);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="form-control" id="viewConfig" rows="10" cols="30"></textarea>
<!-- end snippet -->
Note you can simply use $(this)
to refer to $("#viewConfig")
in the event handler (see the manual).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论