简体   繁体   中英

How to check if the start of a string has specific character using regex in javascript?

I have a requirement where I have to check whether a word has + followed by space as the first two characters using regex.I tried creating regex and used match method, but the method always returns null.Could anyone suggest me how to create regex for the following condition?

getWord: function() {
    var index = this.getIndexOfSelectedText(),

      words = this.state.editorText.slice(0, index.start),

      var selectedWords = words.split('\n').pop();
    console.log('selectedWords', selectedWords);
    var exp = '/^\+\s/'
    console.log(selectedWords.match(exp));
    if (selectedWords.match(exp)) {
      return true;
    }

您可以尝试使用正则表达式^\\+\\s+ ...请在此处进行测试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM