473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Apply text formatting with RegExp

Claus Mygind
571 512MB
I want to convert the text color to red for a sub string found in a larger string.

I use the following search and replace technique
Expand|Select|Wrap|Line Numbers
  1. function HighlightSearchValue(oObj, searchStr, addLineBreak)
  2. {
  3.     var replaceStr = '<span style="color:red;">'+searchStr+'</span>';
  4.     var re = new RegExp(searchStr, "gi");
  5.     var result = oObj.replace(re, replaceStr) + (( addLineBreak) ? '<br/>':'');
  6.  
  7.     return result;
  8.  
  9. }
  10.  
My problem is this, I want the match to be case insensitive, but I want the replacement to be case sensitive.

For example, I want to highlight "Burke" in this string "Thomas Burke Corp."

But the search could be "BURKE" or "burke".

I want the final replacement to be

"Thomas <span style="color:red;">Burke</span> Corp."

The code above does not do that of course. What are some suggestions?
Apr 4 '11 #1

✓ answered by gits

i could think of something like this:

Expand|Select|Wrap|Line Numbers
  1. // test in firebug console
  2.  
  3. var text      = 'Thomas Burke Corp.';
  4. var searchStr = 'BURKE';
  5.  
  6. var re  = new RegExp('(' + searchStr + ')', 'gi');
  7. var ret = text.split(re);
  8.  
  9. for (var i = 0, l = ret.length; i < l; i++) {
  10.     if (re.test(ret[i])) {
  11.         ret[i] = ret[i].replace(re, '<foo>' + ret[i] + '</foo>');
  12.     }
  13. }
  14.  
  15. console.log(ret.join(''));
  16.  
kind regards

3 1428
gits
5,390 Expert Mod 4TB
i could think of something like this:

Expand|Select|Wrap|Line Numbers
  1. // test in firebug console
  2.  
  3. var text      = 'Thomas Burke Corp.';
  4. var searchStr = 'BURKE';
  5.  
  6. var re  = new RegExp('(' + searchStr + ')', 'gi');
  7. var ret = text.split(re);
  8.  
  9. for (var i = 0, l = ret.length; i < l; i++) {
  10.     if (re.test(ret[i])) {
  11.         ret[i] = ret[i].replace(re, '<foo>' + ret[i] + '</foo>');
  12.     }
  13. }
  14.  
  15. console.log(ret.join(''));
  16.  
kind regards
Apr 6 '11 #2
Claus Mygind
571 512MB
Wow that was fantastic.

Here is my final code
Expand|Select|Wrap|Line Numbers
  1. function HighlightSearchValue(oObj, searchStr, addLineBreak)
  2. {
  3.     /*
  4.     -------------------------------------------
  5.     this function higlights the section of text
  6.     which the user entered as a search criteria
  7.     -------------------------------------------
  8.     */
  9.  
  10.     var re  = new RegExp('(' + searchStr + ')', 'gi');
  11.  
  12.     var ret = oObj.split(re);
  13.  
  14.     for (var i = 0, l = ret.length; i < l; i++) {
  15.         if (re.test(ret[i])) {
  16.             ret[i] = ret[i].replace(re, '<span style="color:red;">' + ret[i] + '</span>');
  17.         }
  18.     }
  19.     var result = ret.join('')+(( addLineBreak) ? '<br/>':'');
  20.  
  21.     return result;
  22.  
  23. }
  24.  
Apr 7 '11 #3
gits
5,390 Expert Mod 4TB
:) ... glad that it helped :)

kind rgards
Apr 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
by: Blaine76 | last post by:
Hi All, This may seem like a stupid question but is there a way to keep text formatting when you add it to a database? Such as Paragraphs and line breaks? Any help greatly appreciated. Blaine
1
by: Pete from Boston | last post by:
I'm going through some "learning by doing" of javascript through trial and error, and admittedly have a lot to learn. But in taking various scripts and dissecting them, I still can't figure out if...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
1
by: Chuck Bowling | last post by:
Up front, i'm not very good with serialization and i'm not quite sure what capabilities it has. I have an app that outputs a text file with this format: <root> \t <node1> \t\t text... \t...
0
by: vijay mathews | last post by:
hi i am developing a chat application tool for a company i have a problem..... i have written codings for text formatting such as color, size etc in vb.net..... but when it is connected to a...
1
bartonc
by: bartonc | last post by:
Did you know that it's perfectly legal to defer text formatting until you have the value(s) available? Watch this: s = "%d apples plus %d apples equals %d apples." yours = 3 mine = 4 print s...
3
by: =?Utf-8?B?b24tbGluZSBqb3VybmFsIGVkaXRvcg==?= | last post by:
I can't seem to cut-and-paste text, with either .doc or .html formatting, into my .asp web-pages --all the formatting is lost. Is there some code that needs to be added to the page that will...
8
by: codicecrm | last post by:
Hello! I am trying to apply conditional formatting to several subreports within a main report (Charlie) with code - because I have four conditions. When the value of txt_SafetyFundingNumber is ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.