I'm trying to match everything between first and last occurrence with Javascript's .match() or .replace() function. I will therefore be using regex to find what I need to remove. Content will be different with many lines and other characters, but this is just an example.
[quote=Person 1]
[quote=Person 2]
[quote=Person 3]
Person 3
[/quote]
Person 2
[/quote]
Person 1
[/quote]
I need to match and remove everything from second occurrence [quote=Person 2] till second last [/quote]
With that said, it should output this in the end:
[quote=Person 1]
Person 1
[/quote]
You can match the quote tags like this
\[quote(.*?)\]
and the end quote tag like this
\[\/quote\]
Here you go (where x is the above example text):
var newx = x.replace(/^(\[quote=(?:(?!\[quote=)[\s\S]*?))\[quote=[\s\S]+\[\/quote\]\s*([\s\S]+?\[\/quote\]\s*)$/g, "$1$2")
This produces:
[quote=Person 1]
Person 1
[/quote]
Note that x.replace()
doesn't change the contents of x
-- you have to do an assignment like var newx = x.replace(...);
or x = x.replace(...)
to store the modified string.
使用此模式(\\[quote=([^\\]]+)\\])[\\s\\S]+(?=\\s\\2)
并替换为$1
演示
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.