文本框内容正则实时匹配,匹配到的字体高亮

项目中遇到的需求,文本框输入的内容需要实时匹配并高亮
效果如下
在这里插入图片描述
以下代码直接取至项目中此需求核心代码,未做整理
js代码

   // 正则匹配
    matching(){ 
        let {regex,regexRestCorpus}=this.state;
      try{
        const reg = new RegExp(regex.trim(),'gm');
        let result = regexRestCorpus.match(reg);
        let value = regexRestCorpus.replace(reg,'<b>$&</b>');
        if(!regex || !result){
            result = [];
        }
        this.setState({corpus:value,corpusNum:result instanceof Array && result.length,inputTextHeight: 'auto'}, () => {
            const $dom = document.querySelector('#inputText .s-input');
            const inputTextHeight = $dom.clientHeight > $dom.scrollHeight ? $dom.clientHeight : $dom.scrollHeight;
            this.setState({ inputTextHeight });
        })
      }catch(err){
          message.warning('正则表达式不正确!');
          console.log(err)
      }
    }

页面代码:

     <Form.Item  {...formItemLayout} label="正则表达式" >
                        <Input.TextArea rows={4} value={regex} onInput={({currentTarget})=>this.setState({regex:currentTarget.value},()=>this.matching())}/>
                    </Form.Item>
                    <Form.Item  {...formItemLayout} label="正则测试语料" >
                        <span>匹配到<span style={{color: "red"}}>{corpusNum}</span>个结果</span>
                        <div className='textareaBox' id="inputText">
                            <pre style={{ height: inputTextHeight }} dangerouslySetInnerHTML={{__html:corpus}}></pre>
                        <Input.TextArea style={{ height: inputTextHeight }} className='s-input' rows={4} value={regexRestCorpus} onInput={({currentTarget})=>this.changeCorpus(currentTarget.value)} />
                        </div>
                    </Form.Item>

样式代码:

.textareaBox{
    border: 1px solid #CCCCCC;
    overflow: auto;
    position: relative;
    height: 110px;
    border-radius: 3px;
    textarea.s-input{
        background: none repeat scroll 0 0 transparent;
        border: 0 none;
        position: absolute;
        left: 0px;
        top: 0px;
        z-index: 2;
        overflow: auto;
        vertical-align: top;
        outline: none;
        resize: none;
        padding: 0;
        margin: 0;
        line-height: normal;
        font-size: 14px;
        min-height: 100%;
        width: 100%;
        text-align: left;
        &:focus
        { 
            border: 0 none;
            outline: none;
            resize: none;
        }
        font: 100% "courier new", monospace;
    }
    pre{
        color: #fff;
        margin: 0;
        padding: 0;
        width: 100%;
        line-height: normal;
        font-size: 14px;
        text-align: left;
        font: 100% "courier new", monospace;
        white-space: unset;
    }
     pre b{
        background: none repeat scroll 0 0 #FFF000;
        color: #FFF000;
        font-weight: inherit;
        &:nth-child(2n) {
            background: none repeat scroll 0 0 #c7e3ff;
            color: #c7e3ff;
        }
    }
}

思路:
1 textarea 和pre 同步宽、高、位置,textarea在pre上方
2 用正则匹配文本框的值,把匹配到的值外部包上html标签(如这里包上的是<b></b>),
3 保持textarea和pre的字体样式统一,设置pre的字体为白色(这里因为整体背景为白色),
4 设置之前匹配替换的标签字体颜色和背景色为黄色(用:nth-child(2n)伪类来实现颜色区分,奇数一个色,偶数一个色)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caperxi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值