473,396 Members | 1,683 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,396 software developers and data experts.

How to modify a RegEx to accept a + sign

Claus Mygind
571 512MB
The function below takes a string and highlights the text within the string that matches the search criteria. It works fine unless the user includes the + sign. Then this line
var re = new RegExp('(' + searchStr + ')', 'gi');
generates a "SyntaxError: invalid quantifier"

So obviously the + has to be escaped \+. Can that be added to the RegEx expression or does that have to be tested and inserted first?



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.  
Jun 10 '13 #1

✓ answered by Dormilich

Can that be added to the RegEx expression or does that have to be tested and inserted first?
if RegExp could mask their special characters, what would there be left from the RegExp?

So I guess you need to do it beforehand. splitting and re-joining the string seems like an easy way for it.
Expand|Select|Wrap|Line Numbers
  1. function escape_plus(str)
  2. {
  3.     return str.split('+').join('\\+');
  4. }

1 1589
Dormilich
8,658 Expert Mod 8TB
Can that be added to the RegEx expression or does that have to be tested and inserted first?
if RegExp could mask their special characters, what would there be left from the RegExp?

So I guess you need to do it beforehand. splitting and re-joining the string seems like an easy way for it.
Expand|Select|Wrap|Line Numbers
  1. function escape_plus(str)
  2. {
  3.     return str.split('+').join('\\+');
  4. }
Jun 10 '13 #2

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

Similar topics

2
by: Ding | last post by:
Hello, I've got a problem with the regular expression api. To illustrate the problem i've made a little example: Matcher m=Pattern.compile("test").matcher("this is a test not");...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
8
by: vbmark | last post by:
I'm new to RegEx in vb.net so I'm not sure how to do this. I want to know if a string contains two minus signs "-". If there are two then I want it to return TRUE. I also need to know if the...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Steve K. | last post by:
I recall a few months ago coming across an article allowing for encoding (or converting?) xml and html documents into sign language as well as brail for deaf and blind people, and that they were...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
2
by: DotNetNewbie | last post by:
Hello, I have a regex and I want to modify the value of a named capture before doing the regex.Replace. So something like: string text = Regex.Replace(oldText, myRegex, @"<a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.