我有一個字串,我想像這樣洗掉這些資料,\(f(x) = (a b)\)
所以我想獲取所有子字串,然后對陣列進行一些操作。但它只給了我一個 stripedHtml。所以無法獲得如何清潔它。只是想洗掉這個方程。
結果將是:這是證據
const filter_data = `<p>\(f(x) = (a b)\)</p><p>\(f(x) = (a db)\)</p><p>\(f(x) = (a d c b)\)</p>
<p>Here’s the evidence.</p>`
var strippedHtml = filter_data.substring(
filter_data.lastIndexOf("\(") 1,
filter_data.lastIndexOf("\)")
);
console.log(strippedHtml)
uj5u.com熱心網友回復:
JS 有一個接受 RegExp 的 replace 方法:
const filter_data = `<p>\(f(x) = (a b)\)</p><p>\(f(x) = (a db)\)</p><p>\(f(x) = (a d c b)\)</p>
<p>Here’s the evidence.</p>`;
var strippedHtml = filter_data.replace(/\<.*?\(.*?>/g, "");
console.log(strippedHtml);
RegExp 搜索 an<后跟 a(然后 an>并用空值替換所有出現。
在您的字串中,它將匹配兩次并進行替換。也許您必須修改 RegExp 以適合您的真實字串,因為它也會匹配包含的文本節點,(但這就是我此時對給定資料所做的事情。
uj5u.com熱心網友回復:
您可以使用以下正則運算式來獲取僅針對您提供的類似型別資料的解決方案
const filterData1 = `<p>\(f(x) = (a b)\)</p><p>\(f(x) = (a db)\)</p><p>\(f(x) = (a d c b)\)</p><p>Here’s the evidence.</p>`
const filterData2 = `<p>\(f(x) = (a b)\)</p><p>\(f(x) = (a db)\)</p><p>\(f(x) = (a d c b)\)</p><p>Here’s the evidence.</p><p>\(f(x) = (a b)\)</p>`
const regEx1 = /<[^>]*>/g //regular expression to remove all html tags
const regEx2 = /\([^\)]*\)/g //regular expression to remove everything between \( and \)
const regEx3 = /[=)]/g //regular expression to remove = and )
const result1 = filterData1.replace(regEx1,'').replace(regEx2,'').replace(regEx3,'').trim()
const result2 = filterData2.replace(regEx1,'').replace(regEx2,'').replace(regEx3,'').trim()
console.log("Result1 : ",result1);
console.log("Result2 : ",result2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/410766.html
標籤:
