How can I test whether a string contain 1 or more than one dot put together. Please show me where I went wrong in the code below.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let text = "Is this .. all there is?";
let pattern = /[^.+]/g;// not allow more than one dot put tothether
//should return false but instead return true
let result = pattern.test(text)
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
3
Answers
Quantifier
{}
represents the length two or more and you can only set string as inner HTML content not boolean like true or false directly.Use the
^
and$
sytnax, signifying the start and end of line.Your regular is not right for your purpose. It matches a single character not present in the list: [^.+] (
.
or+
character), then it will always returntrue
if the string contains any character that is not.
or+
.You could check if string contains more than one dot put together, using this pattern:
/..+/g