我目前解決我的NodeJS應用程序,問題和發現這是,我猜,適用於結腸配對的字眼:
([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))
它也符合引用值。像a:"b" c:'d e' f:g
例編碼器ES6:
const regex = /([\w]+:)("(([^"])*)"|'(([^'])*)'|(([^\s])*))/g;
const str = `category:"live casino" gsp:S1aik-UBnl aa:"b" c:'d e' f:g`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
例在PHP編碼
$re = '/([\w]+:)("(([^"])*)"|\'(([^\'])*)\'|(([^\s])*))/';
$str = 'category:"live casino" gsp:S1aik-UBnl aa:"b" c:\'d e\' f:g';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
您可以檢查/使用這個在線工具測試您的正則表達式的表達式:https://regex101.com
順便說一句,如果沒有被regex101.com刪除,你可以瀏覽那個例子編碼here
對於完整的示例+1,但對於一個正則表達式來說太複雜了-1。 ;) – 2010-03-03 11:46:37