简体   繁体   中英

Match a condition on the first line

I have matched all characters that are not tabs in regex.
/[^\\t]/g

I would also like to do the same match but just for the first row. This will allow me to color non-tab characters in the first line as one color (title line) and the remaining with another.

I am able to match the entire first row but cannot match first row non-tabs. Can anyone help? Is this possible.

/^(?!\\n)(.*)/g

http://jsfiddle.net/viciouskinid/w0o8sa7w/

<div id='Highlighter'></div><div id='Highlighter1'></div>
<textarea id='Status'>asdfafd   afsdd   fadsfasd
asdfafd afsdd   fadsfasdasdfafd afsdd   fadsfasd
asdfafd afsdd   fadsfasd
asdfafd afsdd   fadsfasd
asdfafd afsdd   fadsfasd
asdfafd afsdd   fadsfasd
asdfafd afsdd   fadsfasd</textarea>




var highlighter = $('#Highlighter'),highlighter1 = $('#Highlighter1'),
status = $('#Status'),
re = /[^\t]/g, 
re1 = /^(?!\n)(.*)/g;//;




var keyupinput = function (highlighter,status,re) {
var content = status.val(),
    match,
    start = 0,
    output = '';

while (match = re.exec(content)) {
    output += content.substring(start, match.index) + '<b>' + content.substring(match.index, match.index + match[0].length) + '</b>';
    start = match.index + match[0].length;
}
output += content.substring(start, content.length);
highlighter.html(output);
}

status.on('keyup input', keyupinput(highlighter,status,re1));
status.on('keyup input', keyupinput(highlighter1,status,re));

You can't do that with a javascript regex in one shot. So the best compromise is to extract the first line and to make the replacements in it:

var result = str.replace(/^.*/, function (m) { 
    return m.replace(/[^\t\r]+/g, '<b>$&</b>'); }); 

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