search

Home  >  Q&A  >  body text

How to change the text color in a string in javascript

  1. 'Test text test text test text cough word test text test text cough test text test text test text test text cough word test text '

  2. The above text test is used to know the position of the first and third cough text and how to change the color of these two words.

 <textarea class="editor-input" placeholder="请输入内容" v-model="inputValue"></textarea>
<iframe src="about:blank" id="bbb" contentEditable="true" width="100%" height="100%"></iframe>

Currently using this method to solve

巴扎黑巴扎黑2798 days ago915

reply all(3)I'll reply

  • PHPz

    PHPz2017-05-19 10:34:03

    测试文字<span style="color:red">测试文字</span>

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:34:03

    The following replaces the first and third coughs, adding red.

    var str = '测试文字测试文字测试文咳嗽字测试文字测试文字咳嗽测试文字测试文字测试文字测试文咳嗽字测试文字';
    
    var result = str.replace(/(咳嗽)([^咳]+[^咳]+)/, '<span sytle="color:red"></span><span sytle="color:red"></span>');
    
    console.log(result);

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:34:03

    If you know the location, just subscript and add color. You can also use regular filtering

    reply
    0
  • Cancelreply