regex - JavaScript RegExp Special Characters -


the following method highlights substrings of large string. want support special character * represents string of length. example, if * inputted in search text field, whole loaded text file should highlighted. when input becomes *a, words end 'a' should highlighted. 'a' matches words contain a. however, @ moment * character not picked up. can fix problem? in advance..

function search() {     var hid = document.getelementbyid('hidtxt').value;     if(hid.length == 0) hid.value=document.getelementbyid("input").innerhtml;         var text = document.getelementbyid("searchtext").value;        if (!text) return;         var regex =  new regexp(text, 'gi');             document.getelementbyid("input").innerhtml = hid.replace(regex, '<span style="background-color:yellow;">$&</span>');       } 

i don't think can make such straightforward translation of input field regex.

the asterisk on own won't match has .* valid regex.

other cases more complex, e.g. input string blah bla bl bla *837465jfbnrja

.*a match whole string while after bla bla , *837465jfbnrja separate matches (as mention highlighting words)

to make work regex need non-greedy matching of not whitespace character:

\b(?:[^\s])*?a\b

try combinations out here , you'll idea:

http://regexpal.com/

you'd need come fixed set of rules want allow, can prepare translation of allowed special characters regex make work.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -