工作中经常遇到字符串中含有"",由于js动态创建div元素,如果title中含有"",就会出现莫名的错误,由此记录下此bug。 var question = 是否经常感到"压力"或其他。 var newQuestion = question.replace(/\"/g, "-") //把 双引号用-代替 ,当然也可以""代替,表示去除。//检测某字符串中是否包含特殊字符 function special( str ){ var special = RegExp(/[(\ )(\~)(\!)(\@)(\#) (\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=) (\[)(\])(\{)(\})(\|)(\\)(\)(\:)(\')(\")(\,)(\.)(\/) (\<)(\>)(\?)(\)]+/) return ( special.test(str) ) } 返回true说明有特殊符号。直接替换不就完了吗:
d = d.replace("\"","").replace("\"","")
如果不确定有多少个双引号:
d = d.replace(/\"/g, "")