0

必须是简单的修复,但我一辈子都看不到我做错了什么。

我已经在在线测试器中测试了 reg 表达式并且它可以工作,所以假设它是 Flex 依赖的问题。

这是我的代码和跟踪输出 - 只是在我删除一个元素时尝试压缩一个字符串。

trace('Found Job To Delete:' + strJobToDelete);                         

strJobRegExp = "(" + strJobToDelete + "\\, )|(, " + strJobToDelete + ")|( " +  strJobToDelete + ")|(" + strJobToDelete + " )"
trace('Reg exp: ' + strJobRegExp);
findJobRegExp = new RegExp(strJobRegExp);                           
strJobNumList.replace(findJobRegExp, '');
trace('After Remove: ' + strJobNumList);

这是输出:

找到要删除的工作:0007-13

正则表达式: (0007-13\, )|(, 0007-13)|( 0007-13)|(0007-13 )

删除后:0007-13、0006-13

编辑后 ===> 新工作:0007-13、0006-13

4

1 回答 1

0

您正在匹配空格或空格和逗号,但并非没有它们中的任何一个。

试试这个模式:

" 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 "
于 2013-05-24T23:17:16.203 回答