Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查字符串是否仅包含特殊字符_Javascript_Regex_Special Characters - Fatal编程技术网

Javascript 检查字符串是否仅包含特殊字符

Javascript 检查字符串是否仅包含特殊字符,javascript,regex,special-characters,Javascript,Regex,Special Characters,是否可以使用javascript检查字符串是否仅包含特殊字符?匹配字符串中仅包含字符的位置,这些字符不在此处定义为a-z、a-z或0-9的类中 var regex = /^[^a-zA-Z0-9]+$/ 测试它 console.log( "My string to test".match(regex) ) console.log( "My string, to test!".match(regex) ) 是的,这是可能的。试试下面,它应该可以工作 function hasOnlySpecia

是否可以使用javascript检查字符串是否仅包含特殊字符?

匹配字符串中仅包含字符的位置,这些字符不在此处定义为
a-z
a-z
0-9
的类中

var regex = /^[^a-zA-Z0-9]+$/
测试它

console.log( "My string to test".match(regex) )
console.log( "My string, to test!".match(regex) )

是的,这是可能的。试试下面,它应该可以工作

function hasOnlySpecialCharater(val) {
    var pattern = /^[^a-zA-Z0-9]+$/;
    return (pattern.test(val));
}

console.log(hasOnlySpecialCharater("@#$"));
console.log(hasOnlySpecialCharater("ChiragP&^"));
我们可以这样写:

    function checkOnlySplChar(str) {
            // to check if string contains only special characters 
            var alphaNumOnly = RegExp('/^[^a-zA-Z0-9]+$/');
            return (alphaNumOnly.test(str));
        }    

对田地?不是字符串?你用什么语言工作?是的。如果你想要一个更具体的答案,至少告诉我们背景和技术。好。请向我们展示您用于访问该字段的代码,并告诉我们“特殊字符”的确切含义。这不是“仅限特殊字符”