捕获两个特殊字符之间的单词

时间:2015-05-14 14:39:32

标签: javascript jquery

我有一个这样的字符串:

"some string, some $special$ string, some string, some string, some $special$ string,..."

我需要捕获两个$表示法之间的所有单词,并将单词放在<code></code>标记内。上面字符串的结果应该是这样的:

"some string, some <code>special</code> string, some string, some string, some <code>special</code> string,..."

如何通过javascript或jquery执行此操作?

3 个答案:

答案 0 :(得分:3)

您可以使用替换功能和简单的正则表达式:

modelBuilder.Entity<Booking>()
            .HasRequired(b => b.Report) 
            .WithMany(b => b.Bookings) 
            .HasForeignKey(p => p.Report_Id);

答案 1 :(得分:1)

使用foreach fd: find device corresponding to fd call device poll function to setup wait queues (with poll_wait) and to collect its "ready-now" mask while time remaining in timeout and no devices are ready: sleep return from system call (either due to timeout or to ready devices) 中的regex,如下所示:

replace
  1. var myStr = "some string, some $special$ string, some string, some string, some $NotSospecial$ string,..."; myStr = myStr.replace(/\$(\w+)\$/g, '<code>$1</code>'); :这将转移\$完全匹配
  2. $:捕获组:将匹配任何字符。时间
  3. (\w+):全局匹配。在第一场比赛后继续
  4. g:来自捕获组的匹配
  5. 演示:https://jsfiddle.net/tusharj/o5yn5ast/

答案 2 :(得分:0)

使用正则表达式和string.replace的重载,它带有正则表达式和替换函数:

&#13;
&#13;
var re = /\$(.*?)\$/g;
var input = "some string, some $special$ string, some string, some string, some $special$ string,...";
var result = input.replace(re,function(match,g1){
    return "<code>" + g1 + "</code>";
});

alert(result);
&#13;
&#13;
&#13;