简体   繁体   English

如何在正则表达式中的<&>之间进行匹配?

[英]How to match anything between < & > in regex?

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! 任何帮助都将如此巨大! :) :)

It's not clear from your question how you are calling the validateText function. 从您的问题尚不清楚您如何调用validateText函数。 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. 但是似乎正在尝试在功能之外设置targeting ,这意味着您可能要在框中输入文字之前进行设置。

Below I change val to val() to call the function and looked up the value when the function runs rather than before. 下面,我将val更改为val()来调用函数,并在函数运行时(而不是之前)查找值。 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM