jQuery Data Type How to - Check if two characters are separated in a specific way in a string








Question

We would like to know how to check if two characters are separated in a specific way in a string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.1.0.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w  ww.j a v a2 s .  co m-->
function ABCheck(str) {
    for (var i=0; i < str.length; i++){
        if ((str.charAt(i) == "a") && (str.charAt(i+4) == "b")){
            return true;
        }
    }
};
$('#value').text(ABCheck('hi borrowed'));
});
</script>
</head>
<body>
  <span id="value">this is atest</span>
</body>
</html>

The code above is rendered as follows: