匹配除了少数字符之外的ascii字符

时间:2016-02-16 08:13:26

标签: javascript regex

我有一个匹配所有ascii字符的正则表达式:

@ApiImplicitParams({
    @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
            value = "Results page you want to retrieve (0..N)"),
    @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
            value = "Number of records per page."),
    @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
            value = "Sorting criteria in the format: property(,asc|desc). " +
                    "Default sort order is ascending. " +
                    "Multiple sort criteria are supported.")
})

现在,我需要从此范围中排除以下字符:/^[\x00-\x7F]*$/ '。我该怎么做?

3 个答案:

答案 0 :(得分:2)

您可以对不允许的字符使用否定前瞻:

/^((?!['"])[\x00-\x7F])*$/

RegEx Demo

(?!['"])在您的输入中禁止使用单引号或双引号是否定的预测。

答案 1 :(得分:2)

您可以通过

排除某个范围内的字符
/^(?![\.])[\x00-\x7F]*$/

(?![\.])作为前缀,从正则表达式匹配中排除.

或在您的方案中

/^(?!['"])[\x00-\x7F]*$/

修改:

将正则表达式包装在大括号中以多次匹配

/^((?!['"])[\x00-\x7F])*$/

答案 2 :(得分:1)

IMO是迄今为止最简单的解决方案:

/^[\x00-\x21\x23-\x26\x28-\x7F]*$/