【问题标题】:String wildcard replace or delete in JavascriptJavascript中的字符串通配符替换或删除
【发布时间】:2020-10-12 16:53:04
【问题描述】:

我想使用 Js 或 reactJS 替换或删除句子中的子字符串。

以下是我的例句。

RAND Corporation is an American nonprofit global policy think tank created in 1948 by Douglas Aircraft Company to offer research and analysis to the United States Armed Forces. 
It is financed by the U.S. government and private endowment, corporations, universities and private individuals

Source: <Origin Href="Link">https://rand.org</a>
Thanks ..... 

在这句话中,我需要找到Source: &lt;Origin Href="Link"&gt;https://rand.org&lt;/a&gt;

所以基本上它会像 Source * &lt;/a&gt; Source & &lt;/a&gt; 将是其他句子中的常见字符串。 * 是通配符,可以包含其他各种文本。

那么,如何删除这一行或用空格或其他东西替换。

我当前的代码是

var sb = "RAND Corporation is an American nonprofit global policy think tank created in 1948 by Douglas Aircraft Company to offer research and analysis to the United States Armed Forces. It is financed by the U.S. government and private endowment, corporations, universities and private individuals Source: <Origin Href=\"Link\">https:\/\/rand.org</a> Thanks ..... "

var result = sb.replace(/\nSource:.*<\/a>/, '')

alert(result)

【问题讨论】:

标签: javascript reactjs regex


【解决方案1】:

删除整行?只需使用正则表达式:/\nSource:.*&lt;\/a&gt;/. 匹配任何非换行符,* 表示重复 0 次或更多次,&lt;/a&gt; 中的 / 需要转义)

yourString.replace(/\nSource:.*<\/a>/, '')

【讨论】:

  • 我没有得到答案......我在我的问题中添加代码
  • @MuneebK 您剥离了 lineBreak 字符,我的正则表达式尝试在开头匹配一个以完全删除该行,而不是将其留空。如果您不知道我的意思,请尝试阅读一下 JavaScript 中的 RegExp
  • 是的,我明白了。当源出现时,这将适用于新行。句子中的anywere怎么样
猜你喜欢
  • 2014-02-24
  • 2021-03-20
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
相关资源
最近更新 更多