Javascript RegExp and boundaries

前端 未结 5 622
北恋
北恋 2021-01-15 16:48

A colleague asked me about a Regular expression problem, and I can\'t seem to find and answer for him.

We\'re using boundaries to highlight certain lengths of text i

5条回答
  •  温柔的废话
    2021-01-15 16:59

    There are 2 ways to write your regular expressions in Javascript

    1. literal
    2. RegExp object

    In literal way, you use as you learned in your textbook, e.g. /balabala/ But in RegExp object, regular expression is written as a string.

    Try the following codes, you know what string behaves in javascript.

    alert("O\K");
    alert("O\\K");
    

    There's another occasion when Regexp written in a textarea or input box. For example,

    http://www.pagecolumn.com/tool/regtest.htm

    In this case, \ in Regexp need not be escaped.

提交回复
热议问题