0

I'm trying to match anything that lies between < and >, and nothing seems to be working.

My current code is:

    var regex = /\<(.*?)\>/
    var targeting = $('#auto-expand').val     //A text area
    function validateText(field)
    {
        if (regex.test(field) == true)
            {
                alert(field.match(regex))
            }
        else
            {
                alert("fail")
            }
    }

It keeps returning fail, not sure why. Any help would be so great! :)

akemedis
  • 414
  • 2
  • 6
  • 17

1 Answers1

3

It's not clear from your question how you are calling the validateText function. But it looks like are trying to set targeting outside the function, which means you are probably setting it before there's text in the box.

Below I change val to val() to call the function and looked up the value when the function runs rather than before. The regex itself works fine (keeping this in mind)

var regex = /<(.*?)>/

function validateText() {
  var targeting = $('#auto-expand').val() //A text area

  if (regex.test(targeting) == true) {
    alert(targeting.match(regex))
  } else {
    alert("fail")
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<textarea id="auto-expand"></textarea>
<button onclick=validateText()>Test</button>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mark
  • 90,562
  • 7
  • 108
  • 148