文字列が次の文字で始まるかどうかを確認しようとしています: /
どうすればこれを達成できますか?
if(someString.indexOf('/') === 0) {
}
文字列の文字は、添字演算子を介してアクセスできます[]
。
if (string[0] == '/') {
}
[0]
JS ではインデックスが 0 ベースであるため、文字列の最初の文字を意味します。上記は正規表現でも可能です。
の代替String.indexOf
:/^\//.test(yourString)
data.substring(0, input.length) === input
次のサンプルコードを参照してください
var data = "/hello";
var input = "/";
if(data.substring(0, input.length) === input)
alert("slash found");
else
alert("slash not found");
<script>
function checkvalidate( CheckString ) {
if ( CheckString.indexOf("/") == 0 )
alert ("this string has a /!");
}
</script>
<input type="text" id="textinput" value="" />
<input type="button" onclick="checkvalidate( document.getElementById('textinput').value );" value="Checkme" />
var str = "abcd";
if (str.charAt(0) === '/')