Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2009, 04:44 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default RegEx matching in Javascript?

Lets say I have some code....

Code:
<img src="dir/cas.jpg"><img src="dir/ast.jpg">
I want to match this string to an array, using the values in the above code to the variable $matches to return the following:

Code:
matches[0] = cas
matches[1] = ast
Then I want to put all the results into a single string:

Code:
output = cas,ast
How would I do this? I know how to match in PHP, but Javascript is different.

--------------- Added [DATE]1248292603[/DATE] at [TIME]1248292603[/TIME] ---------------

revised my requirements...

--------------- Added [DATE]1248315264[/DATE] at [TIME]1248315264[/TIME] ---------------

So no one has any ideas for me?

This is the last thing I need for an AJAX script I am working on.
Reply With Quote
  #2  
Old 07-23-2009, 06:24 AM
Deceptor's Avatar
Deceptor Deceptor is offline
 
Join Date: Dec 2008
Location: England
Posts: 514
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
<script type="text/javascript">
<!--

var teststring = '<img src="dir/cas.jpg"><img src="dir/ast.jpg">';

function fetch_image_names(code)
{
	matches = new Array();

	while (code.match(/(<img src="dir\/(.+?)\.jpg">)/i))
	{
		matches.push(RegExp.$2);

		code = code.replace(RegExp.$1, '', code);
	}

	return matches;
}

alert('The string yielded the following results: ' + fetch_image_names(teststring).join(','));

-->
</script>
Should do the job for you, following the exact code you posted that is.
Reply With Quote
  #3  
Old 07-23-2009, 01:54 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This works great... But I am having one issue incorporating this with Ajax...

This is the text field to be edited...
Code:
<div class="editUse"><img src="ranking/usage/amy.gif"></div>
When I click on the div, the following Javascript process runs:
Code:
function editBox_use(actual) {
	if(!changing) {
		actual.innerHTML = fetch_usage(actual.innerHTML).join(',');
		actual.innerHTML = "<input id=\""+ actual.id +"_field\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onblur=\"return fieldBlur(this,'" + actual.id + "');\" style=\"width:50px; text-align:center; font-size:12px; \" />";
		changing = true;
	}
	actual.firstChild.focus();
}

function fetch_usage(code) {
	matches = new Array();
	while (code.match(/(<img src="\w+\/usage\/(\w+?)\.gif">)/i)) {
		matches.push(RegExp.$2);
		code = code.replace(RegExp.$1, '', code);
	}
	return matches;
}
This works great, and the text box correctly fills out with "amy". After I edit the box, the following PHP script is run:
($input is the new value where "amy" once was, lets say for now it is "cas")
Code:
function update_score($input)
{
	global $thumbdir;

	$input = str_replace(' ','',$input);
	$input = strtolower($input);

	$uses = explode(',',$input);

	foreach ($uses as $use)
	{
		$usage .= '<img src="'.$thumbdir.'/usage/'.$use.'.gif">';
	}

	echo $usage;
}
Running this script properly and returns the image for "cas" right on the page:
Code:
<div class="editUse"><img src="ranking/usage/cas.gif"></div>

However, now a new issue comes up... THEORETICALLY, if I click on the div AGAIN (without refresshing the page), it should return the text box filled out with the word "cas". It doesn't instead it gives me a blank text box. If I refresh the page, it works fine, but if I don't (which is the purpose of AJAX), the regex script fails.

Is there something wrong with the way the new HTML is echoed out that makes it fail the matching?
Reply With Quote
  #4  
Old 07-23-2009, 03:36 PM
1Unreal 1Unreal is offline
 
Join Date: Jul 2008
Location: London
Posts: 372
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

w3schools covers this nicely http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Reply With Quote
  #5  
Old 07-23-2009, 08:29 PM
Deceptor's Avatar
Deceptor Deceptor is offline
 
Join Date: Dec 2008
Location: England
Posts: 514
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Where do you insert the returned HTML? Obviously when AJAX is returning the response and you're inserting that response it's not doing so correctly, that or the original HTML is stored in a variable and not grabbed each time it's ran.

I'd probably have to see the page with the JS to get the full scope and identify the issue.
Reply With Quote
  #6  
Old 07-24-2009, 02:30 AM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Okay... I have set up a test page... (IE only)
http://www.8wayrun.com/test.html

It uses 2 other files... the test.JS file (link on the page above), and a test.PHP file: contents below:

Code:
<?php

$field = explode('_',$_GET['fieldname']);
$input = $_GET['content'];

if ($field[0] == "score") { update_score($field,$input); }

function update_score($field,$input)
{
	global $vbulletin, $thumbdir;

	$input = str_replace(' ','',$input);
	$input = strtolower($input);

	if (!$input) { echo '&nbsp;'; exit; }
	$uses = explode(',',$input);

	foreach ($uses as $use)
	{
		$usage .= '<img src="ranking/usage/'.$use.'.gif">';
	}

	echo $usage;
}

?>


If you to that page, you will understand my problem more. If you click on the image bar, it will return the following text: "cas,sig,hil,cer,xia". You can then change the text, for instance, type in "tir,tal,tak". Instead of those 5 previous images, you will get 3 different images. That is working as intended...

Now, click on the image bar again, without refreshing the page. Instead of it returning "tir,tal,tak", it returns an empty text box. This is my problem.
Reply With Quote
  #7  
Old 07-24-2009, 04:27 PM
Deceptor's Avatar
Deceptor Deceptor is offline
 
Join Date: Dec 2008
Location: England
Posts: 514
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Change:
Code:
	while (code.match(/(<img src="\w+\/usage\/(\w+?)\.gif">)/i)) {
To:
Code:
	while (code.match(/(<img src=".+?\/usage\/(\w+?)\.gif">)/i)) {
When the HTML is returned through AJAX, the browser renders the image path as the relative URL, meaning the pattern \w+ will fail the second time, the greedy .+? fixes this.
Reply With Quote
  #8  
Old 07-24-2009, 06:59 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Wow... that really fixed it... thanks so much...

--------------- Added [DATE]1248471182[/DATE] at [TIME]1248471182[/TIME] ---------------

I'm having problems with this code...

Code:
function fetch_usage(code) {
	matches = new Array();
	while (code.match(/(<img src="\w+\/usage\/(\w+?)\.gif">)/i)) {
		matches.push(RegExp.$2);
		code = code.replace(RegExp.$1, '', code);
	}
	return matches;
}
Works in IE... doesn't work in FF.
Reply With Quote
  #9  
Old 07-26-2009, 10:47 AM
Deceptor's Avatar
Deceptor Deceptor is offline
 
Join Date: Dec 2008
Location: England
Posts: 514
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That code you posted is the old function, did you mean the new one with the edit I specified above?
Reply With Quote
  #10  
Old 07-26-2009, 02:25 PM
Jaxel Jaxel is offline
 
Join Date: Sep 2005
Posts: 1,160
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I figured out the problem... this line does not work in firefox...

Code:
code = code.replace(RegExp.$1, '', code);
I changed it to:

Code:
code = code.replace(RegExp.$1, '');
And now it works fine.

This is what I used this for: http://www.8wayrun.com/video.php?do=...s&videoid=1045 (watch the video)
Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:14 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04119 seconds
  • Memory Usage 2,257KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (14)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete