Current location: Hot Scripts Forums » General Web Coding » JavaScript » Split string but keep entities


Split string but keep entities

Reply
  #1 (permalink)  
Old 06-13-10, 06:07 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Split string but keep entities?

Hello,
I'm about splitting a string and I use the function split. But my string contains entities ( decimal, hexadecimal and named entities ) and this function breaks my text. So are there any way to split a string but keep entities remain? Thanks.

Code:
Hello mấn

Last edited by Mythvn; 06-13-10 at 06:12 AM.
Reply With Quote
  #2 (permalink)  
Old 06-13-10, 08:29 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
What do you want the string to be split into?
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 06-13-10, 09:45 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
I want this

Code:
Hello mấn
into this

Code:
H
e
l
l
o
 
m
ấ
n
Is that possible?
Reply With Quote
  #4 (permalink)  
Old 06-13-10, 02:08 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
I don't think it's possible using existing javascript functions, but you can create one yourself.

You just need a for loop within you check if the current character is an ampersand or not. If so, you read the next couple of characters until a ; is found. All characters in between those 2 should then go into 1 array item, and then you move on to the next character.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #5 (permalink)  
Old 06-14-10, 03:02 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by UnrealEd View Post
I don't think it's possible using existing javascript functions, but you can create one yourself.

You just need a for loop within you check if the current character is an ampersand or not. If so, you read the next couple of characters until a ; is found. All characters in between those 2 should then go into 1 array item, and then you move on to the next character.
I have thought about this solution before but a valid entity can an ampersand, a couple of characters ( a-z 0-9 # ) followed and a semi-colon. To validate this condition in a "for" loop can be complex. So I try to use RegEX with split function.

Code:
var regex = new RegExp("\(&([^;]{6}))", "g");
myArray = string.split(regex);
for (i = 0; i < myArray.length; i++) {
	alert("--" + myArray[i] + "--");
}
The RegEX seems to be not so effective. Can you give me a suggestion for it?
Reply With Quote
  #6 (permalink)  
Old 06-14-10, 06:56 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
try this regex:
Code:
/(&#?[a-z0-9]+;)/
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP C-Sharp DB Access Wrapper Question digioz ASP.NET 1 09-08-08 09:54 AM
search function using dot net nishudude_13 Windows .NET Programming 1 08-28-08 11:28 AM
[SOLVED] searching through a grid view painthu ASP.NET 5 05-21-08 10:11 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 02:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.