简体   繁体   English

Javascript:提取字符串中以特定字符开头的单词

[英]Javascript : Extract words starting with specific character in a string

I'm having this string : 我有这个字符串:

Hey I love #apple and #orange and also #banana

I would like to extract every words that start with the # symbol. 我想提取以#符号开头的所有单词。

Currently I'm achieving it with this code : 目前,我正在用以下代码实现它:

var last = 0;
var n = 0;
var str = "Hey I love #apple and #orange and also #banana";
do{
    n = str.indexOf("#", last);
    if(n != -1){
        //The code found the # char at position 'n'
        last = n+1; //saving the last found position for next loop

        //I'm using this to find the end of the word
        var suffixArr = [' ', '#'];
        var e = -1;
        for(var i = 0; i < suffixArr.length;i++){
            if(str.indexOf(suffixArr[i], n) != -1){
               e = str.indexOf(suffixArr[i], n+1);
               break;
            }
        }
        if(e == -1){
            //Here it could no find banana because there isn't any white space or # after
            e = str.length; //this is the only possibility i've found
        }

        //extracting the word from the string
        var word = str.substr(n+1, (e-1)-n);
   }
}
while (n != -1);

How can I manage to find words starting with # and with aZ characters only. 如何设法查找以#开头且仅以aZ characters开头的单词。 If for example I have #apple! 例如,如果我有#apple! i should be able to extract apple And also, as I mentionned in the code, how do I manage to get the word if it appears at the end of the string 我应该能够提取apple而且,正如我在代码中提到的,如果单词出现在字符串的末尾,我如何设法得到它

(?:^|[ ])#([a-zA-Z]+)

Try this.Grab the capture.See demo. 试试看,获取捕捉,请看演示。

https://regex101.com/r/wU7sQ0/18 https://regex101.com/r/wU7sQ0/18

    var re = /(?:^|[ ])#([a-zA-Z]+)/gm;
var str = 'Hey I love #apple and #orange and #apple!@ also #banana';
var m;

while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

You can use the regex /(^|\\s)#[az]+/i and match it and then make use of Array.join (here it is being used internally when + "" executed) and replace all the # from the formed String and split on , 您可以使用正则表达式/(^|\\s)#[az]+/i进行match ,然后使用Array.join (在执行+ ""时在内部使用它)并替换其中的所有#形成的字符串和分裂上,

var arr = (str.match(/(^|\s)#[a-z]+/i)+"").replace(/#/g,"").split(",");

If you also want to match test in Some#test , then remove the (^|\\s) from the regex. 如果您还想在Some#test匹配test ,则从正则表达式中删除(^|\\s)

暂无
暂无

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

相关问题 Javascript正则表达式只匹配以特定特殊字符开头的单词 - Javascript regex match only words starting with a specific special character 从Javascript中的字符串中提取以某个字符开头的单词 - Extract words that starts with a certain character from a string in Javascript 如何使用从特定字符开始的 JavaScript 将字符串拆分为单词,然后在下一个空格处结束? - How can I split a string into words with JavaScript starting at a particular character, and then end at the next space? Javascript / REGEX:删除以字符串中以空格分隔的特定字母(单词)开头的特定文本(单词) - Javascript / REGEX: Delete a specific Text (word) starting with a specific letter inside a String with words separated by spaces 如何使用javascript从起始索引到空白字符提取字符串? - How to extract string from starting index to white space character using javascript? JavaScript RegEx替换字符串中以#开头的单词 - JavaScript RegEx to replace words starting with # in a string Javascript:替换字符串中的特定单词 - Javascript: Replace specific words in string 从 JavaScript 的段落中提取/匹配特定单词 - Extract/Match specific words from a paragraph in JavaScript JavaScript - 在特定字符上拆分字符串 - JavaScript - splitting a string on specific character 使用 JavaScript RegExp 搜索以特定字母开头的单词 - searching for words starting with specific letters using JavaScript RegExp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM