我正在嘗試替換以下字串
\section{Welcome to $\mathbb{R}^n$}
和
<h1>Welcome to $\mathbb{R}^n$</h1>
顯然,問題是我在大括號之間打開{和}。我試過使用類似的東西
strOutput = strOutput.replace(/\\section\*\s*\{([^}]*)\}/g, "<h1>$1</h1>");
在 JavaScript 中,但沒有運氣。我如何繼續接近這個?
uj5u.com熱心網友回復:
下面是一個例子:
let str = String.raw`\section{Welcome to $\mathbb{R}^n$}`
let result = `<h1>${str.match(/(?<={).*(?=})/)}</h1>`
console.log(result)
uj5u.com熱心網友回復:
這個怎么樣:
let str = String.raw`\section{Welcome to $\mathbb{R}^n$}`
let m = str.replace(/\\section\s*\{(.*)}$/g, "$1")
let result = `<h1>${m}</h1>`
console.log(result)
你表達的問題是\*,這意味著一個字面的星號,但除此之外你幾乎擁有它!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/346728.html
標籤:javascript 正则表达式 细绳 代替
