javascript - Regex match start of string inside lookahead -
i trying match word hello on first , third lines:
hello should matched not want hello matched hello should matched
when use ^\s*hello
, things work not want spaces included in result.
(?=\s*)hello
closer matches 3 hellos.
i expected either ^(?=\s*)hello
or (?=^\s*)hello
work, , confused why don't. why don't these work , how can create regex matches only "hello" after whitespace?
create capture group:
str1 = ' hello should matched'; str2 = 'i not want hello matched'; str3 = 'hello should matched'; str1 = str1.match(/^\s*(hello)/); if (str1) { str1 = str1[1]; } else { str1 = ''; } str2 = str2.match(/^\s*(hello)/); if (str2) { str2 = str2[1]; } else { str2 = ''; } str3 = str3.match(/^\s*(hello)/); if (str3) { str3 = str3[1]; } else { str3 = ''; } $('#output').append('str1: ' + str1 + '<br>str2: ' + str2 + '<br>str3: ' + str3 + '');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="output"></div>
Comments
Post a Comment