I want to return true if a given string has only a certain character but any number of occurrences of that character.
Examples:
// checking for 's'
'ssssss' -> true
'sss s' -> false
'so' -> false
Check this
<div class="container">
<form action="javascript:;" method="post" class="form-inline" id="form">
<input type="text" id="message" class="input-medium" placeholder="Message" value="Hello, world!" />
<button type="button" class="btn" data-action="insert">Show</button>
</form>
</div>
JavaScript
var onloading = (function () {
$('body').on('click', ':button', function () {
var a = document.getElementById("message").value;
var hasS = new RegExp("^[s\s]+$").test(a);
alert(hasS);
});
}());
Example http://jsfiddle.net/kXLv5/40/
Just check if anything other than space and "s"
is there and invert the boolean
var look = "s";
if(!new RegExp("[^\s" + look + "]").test(str)){
// valid
}
or check if they're the only one which are present with the usage of character class and anchors ^
and $
var look = "s";
if(new RegExp("^[\s" + look + "]$").test(str)){
// valid
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With