码迷,mamicode.com
首页 > 其他好文 > 详细

自定义函数使字符串中的某个字符更改样式

时间:2019-02-15 15:54:41      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:font   inf   字符串   定义   regexp   com   一个   nbsp   color   

设定现在有一个字符串 str = “hello, kitty is not cat”;

假设要把其中的 is 变成红色样式:

    先自定义一个函数 red();

    red(str,redstr)函数传递两个参数,一个是整个字符串,另一个是要变样式的字符串。  

    var str = "hello, kitty is not cat";
    var red = (a, redstr = "参数默认") => {
      let zzr = new RegExp("(" + re +")" , "g");
      let newstr = a.replace(zzr, "<font color=red>$1</font>");
      document.write(newstr);
    }
    red(str, "is");

    效果就显示出来了

技术图片

 

    

自定义函数使字符串中的某个字符更改样式

标签:font   inf   字符串   定义   regexp   com   一个   nbsp   color   

原文地址:https://www.cnblogs.com/fulangren/p/10383574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!