Javascript 如何判断字符串是否同时包含单引号 (') 和双引号 (")?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4935736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:50:53  来源:igfitidea点击:

How can I tell if string contains both a Single-quote (') and a double-quote (")?

javascript

提问by Tree

How can I check if string contains both a Single-quote (') and a double-quote ("), like the one below?

如何检查字符串是否同时包含单引号 ( ') 和双引号 ( "),如下所示?

var str = "test'\"";

回答by kmfk

A quick way to check if the string contains both a single quote and a double quote.

检查字符串是否同时包含单引号和双引号的快速方法。

if (str.indexOf('\'') >= 0 && str.indexOf('"') >= 0) {
   //do something
}

edit: if the character is in the first position, indexOf will return zero.

编辑:如果字符在第一个位置,indexOf 将返回零。

回答by Ash Burlaczenko

Try this

尝试这个

var str = "test'\"";

if((str.indexOf('\'') > -1) && (str.indexOf('"') > -1))
{
    //Code here
}

Hope this helps.

希望这可以帮助。

回答by scrappedcola

I'm guessing you want something like /['||"]/.test(str);

我猜你想要类似的东西 /['||"]/.test(str);