gpt4 book ai didi

javascript - Jquery 替换有前缀的单词

转载 作者:行者123 更新时间:2023-11-28 19:37:29 25 4
gpt4 key购买 nike

我想问一下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>

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

最佳答案

如下:

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

关于javascript - Jquery 替换有前缀的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25640584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com