Current location: Hot Scripts Forums » General Web Coding » JavaScript » search in string


search in string

Reply
  #1 (permalink)  
Old 01-24-11, 06:11 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
search in string

Hi there,

I'm looking for a piece of code to search for the parts: "[kort] text [/kort]" in my string.
Between the tags is a variable text that I should extract.

I got it like this, but it's always false:

Code:
var my_str = document.getElementById('extrainfo').value;
var str = "~(?<=[kort]).*(?=[\/kort])~";			

if(my_str.search(str)==-1){
	alert("Not Found inside string");
} else {
	alert("Found inside the string and the location of the sub string is ");
}
anybody a suggestion how to solve this?

_j
Reply With Quote
  #2 (permalink)  
Old 01-24-11, 06:42 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
The search method of a string expects a pattern, not a string.

Lookbehind assertions aren't supported in javascript, so you can't use <?=. Searching for that piece of text doesn't require lookahead and lookbehind assertions. The following pattern should do just fine:
Code:
/\[kort\][^\[]*\[\/kort\]/
I did make a small change to the selected data from the pattern compared to your regex: it's less greedy. Using my pattern it will only match text between 1 [kort] - [/kort] tag-pair. Your pattern would match against the first occurence of [kort] and the last occurence of [/kort], regardless of how many [kort] [/kort]-pairs there are in the string. I think this will give you a little speed boost, as less buffer need to be maintained while processing the regex query
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks


Last edited by UnrealEd; 01-24-11 at 06:46 AM. Reason: added clarification
Reply With Quote
  #3 (permalink)  
Old 01-24-11, 06:50 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
okay,

thnx for the answer. My java shoud look like this (I assume):

Code:
var my_str = document.getElementById('extrainfo').value;
var str = "/\[kort\][^\[]*\[\/kort\]/";			

if(my_str.search(str)==-1){
	alert("Not Found inside string");
} else {
	alert("Found inside the string and the location of the sub string is ");
}
I tried this, but no result:

Code:
var my_str = "[kort]test[/kort]";
			var str = "/\[kort\][^\[]*\[\/kort\]/";
			
			//document.write (my_str.search(str));

			if(my_str.search(str)==-1){
				//alert("Not Found inside string");
				document.write ("no match");
			} else {
				//alert("Found inside the string and the location of the sub string is ");
				document.write ("match");
			}

Last edited by jonnekke; 01-24-11 at 06:53 AM.
Reply With Quote
  #4 (permalink)  
Old 01-24-11, 07:00 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
You should remove the surrounding double quotes around the pattern:
Code:
var str = "/\[kort\][^\[]*\[\/kort\]/";
should be
Code:
var str = /\[kort\][^\[]*\[\/kort\]/;
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #5 (permalink)  
Old 01-24-11, 07:18 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
great, it works!!

now working on extracting the part between [kort] and [/kort].
Going to work on that!


thnx again.
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
[SOLVED] Method to search a string TenFormer PHP 2 09-15-08 07:02 AM
OOP C-Sharp DB Access Wrapper Question digioz ASP.NET 1 09-08-08 09:54 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:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.