您正在匹配空格或空格和逗号,但并非没有它们中的任何一个。
试试这个模式:
" 0007-13(?!,)|, 0007-13|0007-13,? |0007-13"
strJobRegExp = " #(?!,)|, #|#,? |#".replace(/#/g, strJobToDelete);
这(?!,)
是一个否定的前瞻,并表示下一个位置不能有逗号。它不会消耗角色。
结果:
"0007-13" -> ""
"0007-13 0008-13" -> "0008-13"
"0008-13 0007-13" -> "0008-13"
"0007-13 0008-13 0009-13" -> "0008-13 0009-13"
"0008-13 0007-13 0009-13" -> "0008-13 0009-13"
"0008-13 0009-13 0007-13" -> "0008-13 0009-13"
"0007-13, 0008-13" -> "0008-13"
"0008-13, 0007-13" -> "0008-13"
"0007-13, 0008-13, 0009-13" -> "0008-13, 0009-13"
"0008-13, 0007-13, 0009-13" -> "0008-13, 0009-13"
"0008-13, 0009-13, 0007-13" -> "0008-13, 0009-13"
" 0007-13" -> ""
" 0007-13 0008-13" -> " 0008-13"
" 0008-13 0007-13" -> " 0008-13"
" 0007-13 0008-13 0009-13" -> " 0008-13 0009-13"
" 0008-13 0007-13 0009-13" -> " 0008-13 0009-13"
" 0008-13 0009-13 0007-13" -> " 0008-13 0009-13"
" 0007-13, 0008-13" -> " 0008-13"
" 0008-13, 0007-13" -> " 0008-13"
" 0007-13, 0008-13, 0009-13" -> " 0008-13, 0009-13"
" 0008-13, 0007-13, 0009-13" -> " 0008-13, 0009-13"
" 0008-13, 0009-13, 0007-13" -> " 0008-13, 0009-13"
"0007-13 " -> ""
"0007-13 0008-13 " -> "0008-13 "
"0008-13 0007-13 " -> "0008-13 "
"0007-13 0008-13 0009-13 " -> "0008-13 0009-13 "
"0008-13 0007-13 0009-13 " -> "0008-13 0009-13 "
"0008-13 0009-13 0007-13 " -> "0008-13 0009-13 "
"0007-13, 0008-13 " -> "0008-13 "
"0008-13, 0007-13 " -> "0008-13 "
"0007-13, 0008-13, 0009-13 " -> "0008-13, 0009-13 "
"0008-13, 0007-13, 0009-13 " -> "0008-13, 0009-13 "
"0008-13, 0009-13, 0007-13 " -> "0008-13, 0009-13 "
" 0007-13 " -> " "
" 0007-13 0008-13 " -> " 0008-13 "
" 0008-13 0007-13 " -> " 0008-13 "
" 0007-13 0008-13 0009-13 " -> " 0008-13 0009-13 "
" 0008-13 0007-13 0009-13 " -> " 0008-13 0009-13 "
" 0008-13 0009-13 0007-13 " -> " 0008-13 0009-13 "
" 0007-13, 0008-13 " -> " 0008-13 "
" 0008-13, 0007-13 " -> " 0008-13 "
" 0007-13, 0008-13, 0009-13 " -> " 0008-13, 0009-13 "
" 0008-13, 0007-13, 0009-13 " -> " 0008-13, 0009-13 "
" 0008-13, 0009-13, 0007-13 " -> " 0008-13, 0009-13 "