正则表达式:同时匹配单个字符和/或字符组(字符串)?

时间:2011-03-23 12:20:31

标签: php regex preg-replace

我希望能够匹配零个或多个字符列表和/或匹配零个或多个字符串列表。

示例: Here is my 12345 test string 123456.

目标: Here is my 45 test string .

所以我希望删除123,但只有456才会删除它。

我尝试了/[123]+456+/gs之类的东西,但我知道这是错误的。

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

/[123]|456/可能就是您想要的。

答案 1 :(得分:1)

代码:

$str = "Here is my 12345 test string 123456.";
$res = preg_replace('/[123]|456/','',$str);
echo $res;

输出:

Here is my 45 test string .

答案 2 :(得分:0)

对于javascript,这有效:

var oldString = 'Here is my 12345 test string 123456.';

var newString = oldString.replace(/[123](456)?/g, '');