I tested the following code and it returned me false when I expected it to be true..How can I fix this and why it’s returning me false?
<script type="text/javascript">
var str3 ="-12346dsfs56";
var reg4 =/^(-{1})\d+$/;
alert(reg4.test(str3));
</script>
The regex returns False because that string is not a number (it contains letters)!
If you drop the
$, the regex will only check if the string begins with a-and at least one digit. And you can drop the{1}, it’s unnecessary since one occurrence is the default for a regex token.