简体   繁体   English

如何从输入中获取@name的数组索引?

[英]How to get the array index of an @name from an input?

I have a textarea and a dropdown set. 我有一个textarea和一个下拉菜单。 I have it set so if a user does @name (or anything) it will display a dropdown list of users with specified input. 我设置了它,所以如果用户执行@name (或任何),它将显示具有指定输入的用户的下拉列表。 I don't know how to do the regex for searching an array for an @name within it. 我不知道如何在数组中搜索@name中的@name

Example: User types "Hi @bob" , then array is ["Hi","@bob"] ; 示例:用户类型"Hi @bob" ,然后数组为["Hi","@bob"] ;

How do I find where @bob is and if a user hit spaces afterwords, the regex detects if a space is placed right after it. 我如何找到@bob位置,如果用户点击空格后,正则表达式会检测是否在其后面放置了空格。

example code I have tried 我试过的示例代码

$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);

 if(keycode == 64) { //Enter keycode
 $('.postingArea').on("keyup", function(){

    var inputVal = $(this).val();
    var res = inputVal.split(" ");
    console.log(jQuery.inArray( "@/w/", res ));

var myString = inputVal.match(/@([^ ]*)/)[1]; 
 var resultDropdown = $(this).siblings(".result2");


if (jQuery.inArray( "@", res ) < 1) {
        resultDropdown.empty();
    }else {

        $.get("../Admin/Users/SearchUser.php", {userAtd: myString}).done(function(data){
            // Display the returned data in browser
            resultDropdown.html(data);

        });
    } 
 })
}  

This sort of works, but jQuery.inArray doesn't accept regex, so it only works when @ is clicked, but not for the rest of the letters following. 这种工作,但jQuery.inArray不接受正则表达式,所以它只在@被点击时才有效,但不适用于其余的字母。 And then it should detect when there is a space after the word so then it knows that the @ is done. 然后它应该检测到单词之后何时有空格,然后它知道@已完成。

When doing console.log(res); 在做console.log(res)时; The output in log is ["this", "is", "a", "test", "@user"] What I need is for it to detect when @ is clicked and then when that part of the array is finished such as hitting space since hitting space makes the array res now ["this", "is", "a", "test", "@user",""] 日志中的输出是[“this”,“is”,“a”,“test”,“@ user”]我需要的是检测@被点击的时间,然后当数组的那部分完成时因为击中空间因为击中空间使阵列res现在[“this”,“is”,“a”,“test”,“@ user”,“”]

You could use the following regex to extract every words that start by "@" and that have space after it in a string. 您可以使用以下正则表达式提取以“@”开头且在字符串后面有空格的每个单词。

/@[az\\d]+/ig

See examples : https://regex101.com/r/WDZNyl/1 请参阅示例: https//regex101.com/r/WDZNyl/1

Once you get the list, it will be easier for you to find the desired values in database. 获得列表后,您将更容易在数据库中找到所需的值。

I figured out the answer, @Flo's response helped a bit coming up with this. 我想出了答案,@ Flo的回应对此有所帮助。 I had to detect two different array lengths and compare the two and find where the @ started between both arrays and then set a variable x to the length of the original input and then do a .test as my if statement. 我必须检测两个不同的数组长度并比较两者并找到两个数组之间@开始的位置,然后将变量x设置为原始输入的长度,然后执行.test作为我的if语句。 Code down below 代码如下

// @'d feature
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);

 if(keycode == 64) { //Enter keycode
 $('.postingArea').on("keyup", function(){

    var inputVal = $('.postingArea').val();
    var res = inputVal.split(" ");

 var res2 = inputVal.match(/@[a-z\d]+/ig);

 var resultDropdown = $(this).siblings(".result2");
var x=0;
if(x <= res2.length){
   x = res.length-1;
}

if(/@[a-z\d]+/ig.test(res[x])) {
  var myString = res[x]; 

        $.get("../Admin/Users/SearchUser.php", {userAtd: myString}).done(function(data){
            // Display the returned data in browser
            resultDropdown.html(data);

        });


    }else {



          resultDropdown.empty();

    } 
  })
  }
})

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

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