Hello everyone,
I am using a regular expression to parse a text string into various parts -- for ex: string "How do you do" will be changed to array with all the words and white spaces.
I am using the following code (which has been copied from internet) -
<html>
-
<body>
-
-
<script type="text/javascript">
-
-
var text = "Hello how@are you.com";
-
var result = tokenize(text,true,true);
-
document.write(result.join(','));
-
-
function tokenize(text,capture,noflatten)
-
{
-
_normalizer_regex_str='(?:(?:^| +)["\'.\\-]+ *)|(?: *[\'".\\-]+(?: +|$)|@| +)';
-
_normalizer_regex=new RegExp(_normalizer_regex_str,'g');
-
_normalizer_regex_capture=new RegExp('('+_normalizer_regex_str+')','g');
-
return(noflatten?text:flatten_string(text)).split(capture?_normalizer_regex_capture:_normalizer_regex);
-
}
-
-
function flatten_string(text)
-
{
-
var accents={a:/à |á|â|ã|ä|Ã¥/g,c:/ç/g,d:/ð/g,e:/è|é|ê|ë/g,i:/ì|Ã*|î|ï/g,n:/ñ/g,o:/ø|ö|õ|ô|ó|ò/g,u:/ü|û|ú|ù/g,y:/ÿ|ý/g,ae:/æ/g,oe:/Å“/g}
-
-
text=text.toLowerCase();
-
for(var i in accents)
-
{
-
text=text.replace(accents[i],i);
-
}
-
return text;
-
}
-
</script>
-
-
</body>
-
</html>
-
This code is working fine in Mozilla Firefox 2.0 but not working fine in IE 7.0.
If you execute this code, you will see that the result in both browsers are different.
While firefox also returns the splitting delimiters as a part of the array, IE 7.0 seems to ignore the delimiters and simply pass back the array without the delimiters.
I am new to regular expresssions and not able to find out how this regular expression works (since it has been copied from internet).
If someone can help me fix the above code to return same results in case of IE7 and Firefox, that would be great help.
Thanks,
Rupinder
3 8882 acoder 16,027
Recognized Expert Moderator MVP
Changed the thread title to better describe the problem.
Read about regular expressions in Javascript here.
Hello Everyone,
I was able to find the solution to the problem. The original coder on the internet has extended the String.split function to achieve proper functionality.
Sharing it below for others to use: -
String.prototype._split=String.prototype.split;
-
String.prototype.split=function(separator,limit)
-
{
-
var flags="";
-
if(separator===null||limit===null)
-
{
-
return[];
-
}
-
else if(typeof separator=='string')
-
{
-
return this._split(separator,limit);
-
}
-
else if(separator===undefined)
-
{
-
return[this.toString()];
-
}
-
else if(separator instanceof RegExp)
-
{
-
if(!separator._2||!separator._1)
-
{
-
flags=separator.toString().replace(/^[\S\s]+\//,"");
-
if(!separator._1)
-
{
-
if(!separator.global)
-
{
-
separator._1=new RegExp(separator.source,"g"+flags);
-
}
-
else
-
{
-
separator._1=1;
-
}
-
}
-
}
-
separator1=separator._1==1?separator:separator._1;
-
var separator2=(separator._2?separator._2:separator._2=new RegExp("^"+separator1.source+"$",flags));
-
if(limit===undefined||limit<0)
-
{
-
limit=false;
-
}
-
else
-
{
-
limit=Math.floor(limit);
-
if(!limit)return[];
-
}
-
var match,output=[],lastLastIndex=0,i=0;
-
while((limit?i++<=limit:true)&&(match=separator1.exec(this)))
-
{
-
if((match[0].length===0)&&(separator1.lastIndex>match.index))
-
{
-
separator1.lastIndex--;
-
}
-
if(separator1.lastIndex>lastLastIndex)
-
{
-
if(match.length>1)
-
{
-
match[0].replace(separator2,function(){for(var j=1;j<arguments.length-2;j++){if(arguments[j]===undefined)match[j]=undefined;}});
-
}
-
output=output.concat(this.substring(lastLastIndex,match.index),(match.index===this.length?[]:match.slice(1)));
-
lastLastIndex=separator1.lastIndex;
-
}
-
if(match[0].length===0)
-
{
-
separator1.lastIndex++;
-
}
-
}
-
return(lastLastIndex===this.length)?(separator1.test("")?output:output.concat("")):(limit?output:output.concat(this.substring(lastLastIndex)));
-
}
-
else
-
{
-
return this._split(separator,limit);
-
}
-
}
-
Thanks,
Rupinder
acoder 16,027
Recognized Expert Moderator MVP
Thanks for posting your solution. Glad to hear that you got it working. Post again any time if you have more questions.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael McGarry |
last post by:
Hi,
I am horrible with Regular Expressions, can anyone recommend a book on it?
Also I am trying to parse the following string to extract the number
after load average.
".... load average:...
|
by: Martin Robins |
last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
|
by: Zachary Turner |
last post by:
I am hopeing someone can help me with a regular expression. I want to use
RegExp.Split, to split a string such as the following
text_1 /text_3/text_4/.../text_n/
into an array that contains...
|
by: Craig Buchanan |
last post by:
I have a string in the format "name" <address> that i would like to split
into an array of two values. name should be the first value, address the
second value. what does my regex pattern need to...
|
by: Schorschi |
last post by:
Not having used regular expressions much, I need some help.
Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0"
Need result as array of strings... "This","Guy", "Needs", "Some",
"Help"
...
|
by: moondaddy |
last post by:
I'm writing an app in vb.net 1.1 and I need to parse strings that look
similar to the one below. All 5 rows will make up one string. I have a
form where a use can copy/paste data like what you...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: Steve |
last post by:
Hi All,
I'm having a tough time converting the following regex.compile patterns
into the new re.compile format. There is also a differences in the
regsub.sub() vs. re.sub()
Could anyone lend...
|
by: ahropak |
last post by:
Hi,
I have a question regarding a regular expression within Regex.Split() method which will help me to break each line of code into tokens.
I'm trying to parse some lines of C# source code and...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |