假设我有一个通用字符串
"...&<constant_word>+<random_words_with_random_length>&...&...&..."
我想使用分割字符串
"<constant_word>+<random_words_with_random_length>&"
为此,我尝试了 RegEx 拆分,例如
<string>.split(/<constant_word>.*&/)
不幸的是,这个正则表达式拆分到最后一个“&”,即
"<constant_word>+<random_words_with_random_length>&...&...&"
如果我希望它在获得第一个“&”时拆分,那么 RegEx 代码会是什么?
字符串拆分示例
"example&ABC56748393&this&is&a&sample&string".split(/ABC.*&/)
给我
["example&","string"]
而我想要的是..
["example&","this&is&a&sample&string"]
最佳答案
你可以用问号?
改变贪心:
"example&ABC56748393&this&is&a&sample&string".split(/&ABC.*?&/);
// ["example", "this&is&a&sample&string"]
关于javascript - 在 JavaScript 中使用 RegEx 进行拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14386860/