如何轻松拆分此字符串

时间:2022-04-12 21:47:58

I have the following string:

我有以下字符串:

"<--something--><++something++><**something**>"

The string can have an arbitrary number of "somethings" , even just once.

该字符串可以具有任意数量的“某事”,甚至只有一次。

I need to split it like so:

我需要像这样拆分它:

["<--something-->", "<++something++>", ...]

But I don't know how to proceed best.

但我不知道怎么做得最好。

I would do something like string.split("><") but then I would have:

我会做类似string.split(“> <”)的事情,但后来我会:

["<--something--", "++something++", ...]

And with string.split(/(><)/) I'd get:

并使用string.split(/(> <)/)我得到:

["<--something--", "><", "++something++", "><", ...]

I can think of a few less-than-optimal solutions, but I want really elegant one.

我可以想到一些不太理想的解决方案,但我想要非常优雅的解决方案。

4 个解决方案

#1


3  

var s = '<--something--><++something++><**something**>',
    p = s.match(/(<[^>]+>)/g);
console.log(p); // ["<--something-->", "<++something++>", "<**something**>"]

That's only assuming that each "token" will never have a > within. So, it will fail with the following:

这只是假设每个“令牌”永远不会有一个>内。因此,它将失败,具体如下:

<--some>thing--><++something++><**something**>
       ^ problematic

I would like to stress that if you're using this to parse HTML, stop right there. Regex isn't the right solution if you're looking to grab specific elements out of HTML. instead, place the content in a hidden <div> (or something) and use the native DOM accessors.

我想强调的是,如果你使用它来解析HTML,那就停在那里。如果您希望从HTML中获取特定元素,则正则表达式不是正确的解决方案。相反,将内容放在隐藏的

(或其他东西)中,并使用本机DOM访问器。

#2


4  

You're not splitting the string, you are matching it.

你没有拆分字符串,你正在匹配它。

Try this:

string.match(/<(.)\1[^>]+?\1\1>/g)

This will match <, two of a kind, then find the same two of a kind and > at the end.

这将匹配<,两种类型,然后找到相同的两种和>在最后。

#3


2  

This expression should do it:

这个表达式应该这样做:

"<--something--><++something++><**something**>".match(/<([+*-])\1.*?\1\1>/g)

It matches an opening angle bracket, followed by two identical characters (taken from set of +, - and *, but you could just use . to match any; it ends with the same two characters and a closing angle bracket.

它匹配一个开口尖括号,后跟两个相同的字符(取自+, - 和*的集合,但你可以使用。匹配任何;它以相同的两个字符和一个结束尖括号结束。

#4


-1  

solution:

var a = "<--something--><++something++><**something**>";
a.match(/\<(\-|\+|\*)+something(\-|\+|\*)+\>/g);

result:

["<--something-->", "<++something++>", "<**something**>"]

#1


3  

var s = '<--something--><++something++><**something**>',
    p = s.match(/(<[^>]+>)/g);
console.log(p); // ["<--something-->", "<++something++>", "<**something**>"]

That's only assuming that each "token" will never have a > within. So, it will fail with the following:

这只是假设每个“令牌”永远不会有一个>内。因此,它将失败,具体如下:

<--some>thing--><++something++><**something**>
       ^ problematic

I would like to stress that if you're using this to parse HTML, stop right there. Regex isn't the right solution if you're looking to grab specific elements out of HTML. instead, place the content in a hidden <div> (or something) and use the native DOM accessors.

我想强调的是,如果你使用它来解析HTML,那就停在那里。如果您希望从HTML中获取特定元素,则正则表达式不是正确的解决方案。相反,将内容放在隐藏的

(或其他东西)中,并使用本机DOM访问器。

#2


4  

You're not splitting the string, you are matching it.

你没有拆分字符串,你正在匹配它。

Try this:

string.match(/<(.)\1[^>]+?\1\1>/g)

This will match <, two of a kind, then find the same two of a kind and > at the end.

这将匹配<,两种类型,然后找到相同的两种和>在最后。

#3


2  

This expression should do it:

这个表达式应该这样做:

"<--something--><++something++><**something**>".match(/<([+*-])\1.*?\1\1>/g)

It matches an opening angle bracket, followed by two identical characters (taken from set of +, - and *, but you could just use . to match any; it ends with the same two characters and a closing angle bracket.

它匹配一个开口尖括号,后跟两个相同的字符(取自+, - 和*的集合,但你可以使用。匹配任何;它以相同的两个字符和一个结束尖括号结束。

#4


-1  

solution:

var a = "<--something--><++something++><**something**>";
a.match(/\<(\-|\+|\*)+something(\-|\+|\*)+\>/g);

result:

["<--something-->", "<++something++>", "<**something**>"]