如何使用正则表达式匹配字符串中的多个值

发布于 2024-11-18 11:02:59 字数 374 浏览 4 评论 0原文

嗨,我想突出显示整个金戒指 为此,我正在这样做,

var match = hitem.text[0].match(/<em>(.*?)<\/em>/);
where hitem.text[0]=<em>Gold</em> <em>Ring</em>

但问题是 var match 只获得 Gold,所以我只能突出显示 Gold,我想将其设为一个数组,以便它同时包含 Gold 和 Ring,我该怎么做。 看这里http:http://jsfiddle.net/bhXbh/4/

Hi i want to highlight whole gold ring
for this i am doing as

var match = hitem.text[0].match(/<em>(.*?)<\/em>/);
where hitem.text[0]=<em>Gold</em> <em>Ring</em>

but the problem is var match is getting only Gold so i am able to highlight only Gold, i want to make it an array so that it contains both gold and ring, how can i do it.
look at here http: http://jsfiddle.net/bhXbh/4/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-11-25 11:02:59
var s = "<em>Gold</em> <em>Ring</em>";
var rg = /<em>(.*?)<\/em>/g;

var res = new Array();

var match = rg.exec(s);
while(match != null){
    res.push(match[1])
    match = rg.exec(s);
}

alert(res);
var s = "<em>Gold</em> <em>Ring</em>";
var rg = /<em>(.*?)<\/em>/g;

var res = new Array();

var match = rg.exec(s);
while(match != null){
    res.push(match[1])
    match = rg.exec(s);
}

alert(res);
记忆で 2024-11-25 11:02:59

global 修饰符添加到您的正则表达式中,如下所示:(

var match = hitem.text[0].match(/<em>(.*?)<\/em>/ig);

我还添加了“i”,它代表“忽略大小写”,我认为这也是必要的)

http://jsfiddle.net/bhXbh/19/

Add the global modifier to your regexp, like this:

var match = hitem.text[0].match(/<em>(.*?)<\/em>/ig);

(I also added "i", which stands for "ignore case", I assume that's also necessary)

http://jsfiddle.net/bhXbh/19/

送你一个梦 2024-11-25 11:02:59

您需要使用正则表达式对象 exec 方法迭代所有匹配项,然后将第一个反向引用保存到数组中。另请注意 /g 标志,它使您的正则表达式成为全局的,也就是说,设置为捕获所有匹配项而不仅仅是第一个匹配项:

var str = "<em>Gold</em> <em>Ring</em>";
var matches = [];
var re = /<em>(.*?)<\/em>/g;
while (match = re.exec(str)) { // Continues until no more matches are found
  matches.push(match[1]);      // Adds the first backreference
}
console.log(matches);          // returns ["Gold", "Ring"]

如果您想组合匹配项转换为字符串,当然,您可以执行 matches.join(" ");

其他选项

如果您不介意捕获周围的标签,您可以执行var matches = str.match(re);

如果您只想替换 标签,则可以执行 str.replace(re, "$1");

You'll want to iterate through all the matches using the regular expression objects exec method, and then save the first backref to an array. Note also the /g flag, which makes your regex global, that is, set up to capture all matches rather than just the first one:

var str = "<em>Gold</em> <em>Ring</em>";
var matches = [];
var re = /<em>(.*?)<\/em>/g;
while (match = re.exec(str)) { // Continues until no more matches are found
  matches.push(match[1]);      // Adds the first backreference
}
console.log(matches);          // returns ["Gold", "Ring"]

If you want to combine the matches into a string, of course, you could do matches.join(" ");.

Other options

If you don't mind capturing the surrounding <em> tags, you could do var matches = str.match(re);.

And if you just want to replace the <em> tags, you can do str.replace(re, "$1");.

月野兔 2024-11-25 11:02:59

.NET Framework 包含一个特殊的类,您可以将其用于您的目标 - MatchCollection 类。

来自 MSDN 的示例:

using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumped over the lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }

    }

}
// The example produces the following output to the console:
//       3 matches found in:
//          The the quick brown fox  fox jumped over the lazy dog dog.
//       'The' repeated at positions 0 and 4
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54

.NET Framework is contain a special class which you can use for your goal - MatchCollection class.

Example from MSDN:

using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumped over the lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }

    }

}
// The example produces the following output to the console:
//       3 matches found in:
//          The the quick brown fox  fox jumped over the lazy dog dog.
//       'The' repeated at positions 0 and 4
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文