2

I am trying to take input from the textbox now I want to show an alert if the textbox value matches with the regular expression.

I want to check "1702, Belgium" or "Belgium, 1702" using regex but I am getting null.

<script>
   $(document).ready(function(){
       var r =/+{1702}/;
       var v=$(".a").val();

       alert(v.match(r));
   });
</script>
<body>
   <input type="text" class="a" value="1702 Belgium"/>
</body>
5
  • 1
    Why not to use direct string compare with both the strings "1702 Belgium" and "Belgium".. simply create a array contains both the string and use includes method to check it Commented Jul 7, 2021 at 14:52
  • Please answer if you can solve this problem @Sarath Commented Jul 7, 2021 at 14:53
  • 1
    /+{1702}/ is not a valid regular expression. If you want to check if a string contains 1702, you can just use regex /1702/ or even simplier - v.indexOf("1702") >= 0 Commented Jul 7, 2021 at 15:00
  • 2
    You regex pattern looks very wrong. + is a repetition operator that's supposed to come after a pattern. {<number>} is used for counted repetitions and not for what you are trying to use it. Commented Jul 7, 2021 at 15:01
  • If you're looking for "1702, Belgium" or "Belgium, 1702" then it won't match your value of "1702 Belgium Commented Jul 7, 2021 at 15:20

2 Answers 2

2

Since we have only 2 strings need to be compared, Why cant we compare with array of constants("1702, Belgium" and "Belgium, 1702") instead of using regular expressions.

Comparing to regular expressions the above way is easy to understand.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
       var valuesToCompare = ["1702, Belgium", "Belgium, 1702"]
       
       var v = $(".a").val().trim();
       alert(valuesToCompare.includes(v));
       
       // we can also use indexof to check 
       // alert(valuesToCompare.indexOf(v) !== -1);
   });
</script>
<body>
   <input type="text" class="a" value="1702, Belgium"/>
</body>

Sign up to request clarification or add additional context in comments.

Comments

0

Consider the following example.

$(function() {
  $("input.a").next("button").click(function(event) {
    var currentValue = $("input.a").val();
    var currentIndex = currentValue.indexOf("1702")
    if (currentIndex >= 0) {
      alert("1702 Found at " + currentIndex);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="a" value="1702 Belgium" /> <button>Check</button>

The .indexOf() will give you the position in the String that your string exists. It does the same for an Array. I have moved the code into a Click callback so you can test other strings or check it after something has been changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.