简体   繁体   English

如何更改 javascript 文件的输出格式?

[英]How do I change the format of the output from a javascript file?

Javascript: Javascript:

window.onload=function(){
  chrome.runtime.getBackgroundPage(function(backgroundPage){ 
  var currentUrl = backgroundPage.tabURL;

  console.log(currentUrl);



    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", 'http://127.0.0.1:5000/post?url=websiteurl.com', false ); 
    xmlHttp.send( null );
    console.log(xmlHttp.responseText);
    document.getElementById('currentUrl').innerHTML = xmlHttp.responseText;
    return xmlHttp.responseText;

 })

};

This code produces an output that looks like:此代码生成的输出如下所示:

[Value1],[Value2],[Value3].

How do I change it so the output is formatted like:如何更改它以便输出格式如下:

  **[Value1],

    [Value2],

    [Value3],**

with one word per line and in bold?每行一个字,加粗? I'm sure its really easy, but I don't know how to go about doing it?我相信它真的很容易,但我不知道如何去做?

HTML: HTML:

<div id='currentUrl'>

        <script type="text/javascript" src="popup.js"></script>


        </div>

You should use CSS to make it bold.您应该使用 CSS 使其加粗。 You can use join() if it's an array or replace() if it's a string to replace the , with ,<br>您可以使用join()如果它是一个数组或replace()如果它是一个字符串来替换,,<br>

First to replace all the dots to commas you can use:首先将所有点替换为逗号,您可以使用:

var stringReg= new RegExp(stringtoreplace, '.');
targetstring = targetstring.replace(stringReg, ',');

Then if you want to add new lines within your string you can add:然后,如果您想在字符串中添加新行,您可以添加:

"This is my string \nThis is a newline"

If you need to loop each element you can just append the newline string to the end of each element.如果您需要循环每个元素,您可以将换行符附加到每个元素的末尾。 You can loop the string and every time you find a "]," you can add the newline character to the right of it, you can use regex for this:您可以循环字符串,每次找到“]”时,您都可以在其右侧添加换行符,您可以使用正则表达式:

var stringReg= new RegExp(stringtoreplace, '],');
targetstring = targetstring.replace(stringReg, '],\n');

You can append a string to the front by doing:您可以通过执行以下操作将字符串附加到前面:

var result = str1.concat("**");

Or you could simply do this instead:或者你可以简单地这样做:

var startString = "**"
var endString = "**"
var mainString = "This is the main string"
var newString = startString + mainString + endString

For making it bold, use CSS为了使其加粗,请使用 CSS

I think this is what you are looking for, you can use split() and join() functions to produce the desired output.我认为这就是您要寻找的,您可以使用split()join()函数来产生所需的输出。

 const strgVal = '[Value1],[Value2],[Value3]'; let fromatedSr = '**' + strgVal.split(',').join(',\\n') + '**'; console.log(fromatedSr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM