简体   繁体   English

为什么我不能在 //g 中调用 var;

[英]Why can't I call to a var within a / /g;

I am using JavaScript to include textbox and a textarea.我正在使用 JavaScript 来包含文本框和文本区域。 Button–calls a function to determine if the string in the textbox is found in the contents of the textarea. Button——调用一个函数来判断textbox中的字符串是否在textarea的内容中找到。 Naturally, message to the user if the string was found or not.当然,如果找到字符串,则向用户发送消息。

Here is my code:这是我的代码:

HTML HTML

<textarea id="textbox3" rows="4" cols="50"></textarea>    
<input id="text3" value="" />
<button id="findTextButton" onclick="findText();">
    #2 Find text from textbox in textarea
</button>

JavaScript JavaScript

function findText() {
    var txtThree = document.getElementById("textbox3").value; 
    var searchWord = document.getElementById("text3").innerHTML.value;
    var globalSearch = /[a-z]/g;
    var deliver = txtThree.match(globalSearch);
    if (searchWord === "") {
        alert("Please Enter Text");
    } else {
        alert(deliver);
    }

}

What I wanted to do was to call to the variable searchWord within the globalSearch function, but it's not letting me do that.我想做的是在 globalSearch 函数中调用变量 searchWord,但它不允许我这样做。

Okay, I think I get what you are wanting....you have a textarea and a text input box.好的,我想我得到了你想要的......你有一个 textarea 和一个文本输入框。 you want to grab the text from the input box and see if there is a match inside the textarea?您想从输入框中抓取文本并查看 textarea 内是否有匹配项?

You shouldn't need a RegExp unless you intend to do more complicated things.除非您打算做更复杂的事情,否则您不需要 RegExp。 .search() can take RegExp or String.(In this case the value from "text3"). .search() 可以使用 RegExp 或 String。(在这种情况下是“text3”中的值)。 Also your checking if searchWord is empty TOO early.还要检查 searchWord 是否太早为空。 If Javascript runs into ONE error, the whole thing shuts down.如果 Javascript 遇到一个错误,整个过程就会关闭。

function findText() {
    var txtThree = document.getElementById("textbox3").value; 
    //var searchWord = document.getElementById("text3").innerHTML.value;
    //Take away the .innerHTML above
    var searchWord = document.getElementById("text3").value;
    //var globalSearch = /[a-z]/g;
    //You shouldn't need a RegExp.


    if (searchWord === "") {
        alert("Please Enter Text");
    } else {
        var deliver = txtThree.match(searchWord);
        alert(deliver);
    }
}

I tested this and it works fine for me.我对此进行了测试,对我来说效果很好。 IF this is what you were expecting.如果这是您所期望的。

I will add that @UselessCode 's answer of using new RegExp(searchWord) is a good idea.我要补充一点,@UselessCode 使用 new RegExp(searchWord) 的答案是一个好主意。

In your code, globalSearch is a Regular Expression literal , it never changes, and will always find the same thing.在您的代码中, globalSearch是一个正则表达式文字,它永远不会改变,并且总是会找到相同的东西。 The expression /[az]/g means find a single character between a and z .表达式/[az]/g表示在az之间找到单个字符。 The g modifier makes it a global search , meaning that it will keep searching and matching even after it finds the first match. g修饰符使其成为全局搜索,这意味着即使在找到第一个匹配项后,它也会继续搜索和匹配。

So, when you run txtThree.match(globalSearch) , match will return an array of single-character strings containing all of the letters in txtThree .因此,当您运行txtThree.match(globalSearch)match将返回一个包含txtThree中所有字母的单字符串数组。 For example if txtThree contained the string 'This is a test.'例如,如果txtThree包含字符串'This is a test.' it would return the array [ "h", "i", "s", "i", "s", "a", "t", "e", "s", "t" ] .它将返回数组[ "h", "i", "s", "i", "s", "a", "t", "e", "s", "t" ]

You can't use a RegExp literal to search for something that varies.您不能使用 RegExp 文字来搜索变化的内容。 You can however use the RegExp constructor function to generate a dynamic RegExp once you have your searchWord :但是,一旦有了searchWord您就可以使用RegExp 构造函数来生成动态 RegExp :

var rxSearch = new RegExp(searchWord);
var result = txtThree.match(rxSearch);

You can supply the g modifier as one of the flags in the second parameter to RegExp , but since you are just trying to determine if the string is in there are all, you don't need to keep searching once you find it once.您可以提供g改性剂中的第二个参数的标志之一RegExp ,但因为你只是想确定该字符串是否是有一切,你不需要继续搜索,一旦你找到它一次。

match either returns an array of matches (a single-element array in this case since we didn't use the g modifier) or null if a match was not found. match要么返回匹配数组(在本例中为单元素数组,因为我们没有使用g修饰符)或null如果未找到匹配。 Since arrays are truthy and nulls are falsey we can use result in an if statement to test whether anything was found:由于数组是真值而空值是假的,我们可以在if语句中使用result来测试是否找到了任何东西:

if (result) {
  alert('Found somthing!');
} else {
  alert('It\'s not there.');
}

As Buildersrejected points out , RegEx is a little bit of overkill for this situation.正如Buildersrejected 指出的那样,对于这种情况,RegEx 有点矫枉过正。 Were I approaching this problem from scratch I probably would have used indexOf instead:如果我从头开始解决这个问题,我可能会使用indexOf代替:

if (txtThree.indexOf(searchWord) !== -1) {
  alert('Found somthing!');
} else {
  alert('It\'s not there.');
}

In this situation performance doesn't really matter, but if it did, using indexOf would be faster than useing search .在这种情况下,性能并不重要,但如果确实如此,使用indexOf会比使用search更快。 indexOf actually takes a normal string as a parameter, search takes a regular expression, if a string is provided it is converted to a regular expression before being used. indexOf实际上以普通字符串作为参数, search使用正则表达式,如果提供了字符串,则在使用前将其转换为正则表达式。

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

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