假设我有一个通用字符串。
我希望将字符串使用 分割。
给我
"...&<constant_word>+<random_words_with_random_length>&...&...&..."
我希望将字符串使用 分割。
"<constant_word>+<random_words_with_random_length>&"
我曾尝试使用正则表达式分割(RegEx split)
<string>.split(/<constant_word>.*&/)
这个正则表达式可以分割到最后一个'&',但很遗憾。
"<constant_word>+<random_words_with_random_length>&...&...&"
如果我想要在获取第一个"&"时拆分,那么正则表达式代码会是什么?
例如要对字符串进行拆分的示例:
"example&ABC56748393&this&is&a&sample&string".split(/ABC.*&/)
给我
["example&","string"]
尽管我想要的是...
["example&","this&is&a&sample&string"]
/ABC(?:.*?&){2}/
。(?:)
只是将其包装在一个非捕获组中,因此它可以被视为单个实体。 - Mattias Buelens