実現したいこと
正規表現を使い、[]で囲んだタグとタグの間を抜き出したいと考えています。
試したこと
下のように、単純に[hoge]~[/hoge]の間であれば実現できているのですが、
let regex = /[([a-zA-Z0-9=-_]+)]([^]]+)[/([a-z]+)]/g const text = "[hoge]Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been[/hoge]" // console.log(text.match(regex)); let myArray; while ((myArray = regex.exec(text)) != null) { console.log(myArray) }
[]のタグが入れ子になっている場合の取得の方法がわかりません。
const text = "[hoge]Lorem Ipsum is simply dummy text of the printing and typesetting [industry]. Lorem Ipsum has been[/hoge]"
下のように、入れ子のタグはバックスラッシュでエスケープし否定先読みで取得が出来るのか、試してみたのですが、うまくいきませんでした。
let regex = /[([a-zA-Z0-9=-_]+)][^(x(?=[)+)][/([a-z]+)]/g const text = "[hoge]Lorem Ipsum is simply dummy text of the printing and typesetting [industry]. Lorem Ipsum has been[/hoge]" // console.log(text.match(regex)); let myArray; while ((myArray = regex.exec(text)) != null) { console.log(myArray) }
良い方法ご存知の方がいましたら、教えていただけないでしょうか。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/13 03:21