Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从一个字符串中获取多个子字符串作为数组_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript 从一个字符串中获取多个子字符串作为数组

Javascript 从一个字符串中获取多个子字符串作为数组,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,所以我有这个字符串: <li class="list-group-item"> <a href=https://en.wikipedia.org/wiki/C target="_blank"> <h4>C</h4> </a> <p>C or c is the third letter in the English alphabet and a

所以我有这个字符串:

<li class="list-group-item">
          <a href=https://en.wikipedia.org/wiki/C target="_blank">
            <h4>C</h4>
          </a>
          <p>C or c is the third letter in the English alphabet and a letter of the alphabets of many other writing systems which inherited it from the Latin alphabet.</p>
        </li>

        <li class="list-group-item">
          <a href=https://en.wikipedia.org/wiki/Copyright_infringement target="_blank">
            <h4>Copyright infringement</h4>
          </a>
          <p>Copyright infringement (colloquially referred to as piracy) is the use of works protected by copyright law without permission for a usage where such permission is required, thereby infringing certain exclusive rights granted to the copyright holder, such as the right to reproduce, distribute, display or perform the protected work, or to make derivative works.</p>
        </li>

        <li class="list-group-item">
          <a href=https://en.wikipedia.org/wiki/Copyright_law_of_the_United_States target="_blank">
            <h4>Copyright law of the United States</h4>
          </a>
          <p>The copyright law of the United States is intended to encourage the creation of art and culture by rewarding authors and artists with a set of exclusive rights.</p>
        </li>
问题是它只返回一个值,我想将所有链接作为字符串返回数组。我想我可以把大字符串拆分成更小的子字符串,然后再拆分成更小的子字符串,但感觉有点过头了。有更简单的方法吗?

试试这个:

str = str.replace(/href=\S+/g, (oldLink) => {
    console.log(oldLink);
    const newLink = "https://www";
    return "href=" + newLink;
});

你在什么环境下运行这个?浏览器?您想将链接更改为什么?我正在react中运行它。做npm开始。我想把它改成维基百科:
https://en.wikipedia.org/w/api.php?origin=*&action=opensearch&search=
这样我就可以从API调用中获取HTML,然后使用暗模式引导。简而言之,创建darkmode wiki页面是一个个人项目。这个项目可以在这里看到:你走错了方向。您应该解析此标记,并使用DOM遍历方法修改
href
属性。
str = str.replace(/href=\S+/g, (oldLink) => {
    console.log(oldLink);
    const newLink = "https://www";
    return "href=" + newLink;
});