在Java脚本中,我想检查是否":"是否存在于下面的字符串中。我尝试使用indexOf()但它没有工作。任何人都可以建议。
" LOCAL:ABCD:1234&#34 ;;
答案 0 :(得分:1)
应该有效
"LOCAL:ABCD:1234".indexOf(":") //output 5
检查它是否存在
"LOCAL:ABCD:1234".indexOf(":") > -1 //outputs true
答案 1 :(得分:0)
有很多方法可以实现这一目标:
var yourstring = "LOCAL:ABCD:1234";
var substring = '\:';
if (yourstring.includes(substring)) {
// Yes I Found One \:
} else {
// No \: Found
}
或者您可以尝试匹配功能
if (yourstring.match(/\\:/g)) {
// Found it ...
}
或者最后一个:
if (yourstring.indexOf(substring) > -1) {
// Found it ...
}
答案 2 :(得分:0)
试着帮忙:
//<![CDATA[
// external.js
var doc, bod, htm, C, E;
addEventListener('load', function(){
doc = document; bod = doc.body; htm = doc.documentElement;
C = function(tag){
return doc.createElement(tag);
}
E = function(id){
return doc.getElementById(id);
}
var wtf = E('wtf'), out = E('out'), oS = out.style;
E('test').addEventListener('click', function(){
var m = wtf.value.match(/\\:/);
if(m){
out.innerHTML = "It's a Match"; oS.color = 'green';
// just for testing
console.log(m);
}
else{
out.innerHTML = "It's Not a Match"; oS.color = 'red';
}
});
});
//]]>
/* external.css */
html,body{
padding:0; margin:0;
}
.main{
width:980px; margin:0 auto;
}
#test{
margin-left:5px;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<title>Escape Test</title>
<link type='text/css' rel='stylesheet' href='external.css' />
<script type='text/javascript' src='external.js'></script>
</head>
<body>
<div class='main'>
<input id='wtf' type='text' value='what\:the\:fun' /><input id='test' type='button' value='test' />
<div id='out'></div>
</div>
</body>
</html>