我檢查了幾個與在字串中的 javascript 中洗掉重復單詞(在我的情況下,單詞表示由空格分隔的子字串)相關的帖子。下面的一個 RegEx:/(\b\S \b)(?=.*\b\1\b)/g是我在互聯網上找到的與幾乎所有情況都匹配的其中之一,但它會產生一些我無法找出原因的不匹配。例如,它會洗掉一些字符,例如:, /-在它是字串的一部分(尚未到達空白處)的情況下。我想它必須與單詞邊界元字符有關,\b但我無法找到解決方案。
例如,我有以下字串示例:
123-1 123-2 test-1 test-1 w/e 10/04/20
Company w/e 09/06/20 083020-090620
a/b 01/01
test_1 test_2
a/b a/b
Inv 50049 50049 Inv 50195 PrjPAN02
Inv 51360-1, 51366-7; 51372 Inv 51360-1, 51366-7; 372 PrjPAN02
Inv 51360-1, 51366-7; 51372 51372 Inv 513601, 51366-7; 372 PrjPAN02
55009, 55017, 55022 55001, 55022, 55025
55254, 61 55246,66,69
55733, 41, 44 55727, 45,48
57269, 71,74,75, 57354 57266, 73
57437, 38, 41, 43 57434, 40
w/e 09/20/20 091320-092020
并生成以下輸出。你可以在這里測驗:Regex101
1232 test-1 we 1004/20
Company we 0906/20 083020-090620
ab /01
test_1 test_2
a/b
50049 Inv 50195 PrjPAN02
, ; 51372 Inv 513601, 51366-7; 372 PrjPAN02
513601, ; 51372 Inv 513601, 51366-7; 372 PrjPAN02
55009, 55017, 55001, 55022, 55025
55254, 61 5524666,69
55733, 41, 44 55727, 45,48
57269, 7174,75, 57354 57266, 73
57437, 38, 41, 43 57434, 40
we 09/20 091320-092020
我希望得到以下輸出:
123-1 123-2 test-1 w/e 10/04/20
Company w/e 09/06/20 083020-090620
a/b 01/01
test_1 test_2
a/b
50049 Inv 50195 PrjPAN02
51372 Inv 51360-1, 51366-7; 372 PrjPAN02
51360-1, 51372 Inv 513601, 51366-7; 372 PrjPAN02
55009, 55017, 55022 55001, 55022, 55025
55254, 61 55246,66,69
55733, 41, 44 55727, 45,48
57269, 71,74,75, 57354 57266, 73
57437, 38, 41, 43 57434, 40
w/e 09/20/20 091320-092020
我希望每個以空格分隔的重復字串都將被洗掉,但在某些情況下,ReEx 會洗掉以空格分隔的字串中的斜杠 ( /) 和連字符 ( -) 以及逗號 ( ,)。
我檢查了以下類似的問題,試圖找到匹配所有情況的正則運算式:
- Javascript RegExp Word 邊界 unicode 字符
- 使用 Regex JS 洗掉字串中的重復單詞 [重復]
- 正則運算式查找和洗掉重復詞
uj5u.com熱心網友回復:
字邊界在這里不起作用。用
/(?<!\S)(\S )(?!\S)(?=.*(?<!\S)\1(?!\S))/g
解釋
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t,
\f, and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t,
\f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of look-ahead
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377524.html
標籤:javascript 正则表达式
上一篇:OracleREGEXP_LIKE前瞻和后視解決方法
下一篇:如何不反轉十進制數?
