Jquery替换具有前缀的单词

时间:2014-09-03 09:18:36

标签: javascript jquery

我想问一下jquery如何替换仅以$开头的单词,例如:

//$example and $friend here means nothing just a regular string
var txt = "This is just an $example my $friend";

我想替换以$开头的字符串中的所有单词将替换为html标记,如下所示:

This is just an <span class="php-var">$example</span> my <span class="php-var">$friend</span>

我想在客户端执行此操作,以便保存服务器性能

1 个答案:

答案 0 :(得分:3)

如下所示:

var txt = "This is just an $example my $friend";
var txt = txt.replace(/\$\S+/g,  '<span class="php-var">$&</span>');