Reputation: 165
I would like the search a string without case sensitive however change the sub string with original string's case letters.
var mystring = "FiRst Last";
var term = "first"; // it could be "FIRST" or fIRST
var re = new RegExp("^" + term, "i") ;
//option 1
//var t = mystring.replace(re,"<span style='font-weight:bold; color:Blue;'>" + term + "</span>");
//option 2
var t = mystring.replace(re,term.bold().fontcolor("Blue"));
The above code gives first LAST in blue color, however i want it to be FiRst LAST as mystring's case order
maybe indexof() method can be used however there might be an easy efficient way.
Upvotes: 0
Views: 685
Reputation: 1332
var querystr = 'first';
var output = "FiRst Last";
var reg = new RegExp(querystr, 'gi');
var final_str = output.replace(reg, function(str) {return str.bold().fontcolor("Blue")});
See this following link...
Javascript: highlight substring keeping original case but searching in case insensitive mode
Solution give by user113716 may helpful...
Upvotes: 1
Reputation: 5822
Just use a capture group with your first option
var str = "FiRst Last";
var term = "first";
var t = str.replace( /(first)/i, "<span style='font-weight:bold; color:Blue;'>$1</span>");
t will now contain
"<span style='font-weight:bold; color:Blue;'>FiRst</span> Last"
Upvotes: 0