The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Javascript and Regex. All up ins.

JasconiusJasconius sword criminalmad onlineRegistered User regular
edited June 2009 in Help / Advice Forum
OK so. Regex in Javascript isn't exactly gloriously implemented.


Hypothetical string:
10275:cat40089:10277:20.00-35.00|35.00-50.00:10277:20.00-35.00|35.00-50.00:12277:34|36:12275:C|D

The regex:
/(^|:)[0-9]{5}:[\w\.|-]+/g

The loop, minus a few lines of irrelevant code
        do
	{
				
		result = rgx.exec(hashstring["trail"])[0];
				
		if(result.substring(0,1) == ":")
		{
			result = result.substring(1);
		}
		
		if(result.substring((result.length - 1), result.length) == ":")
		{
			result = result.substring(0, (result.length - 1));
		}
		
		result = result.replace(",", "");
		
		var thesplit = result.split(":");
			
		hs.push({key:thesplit[0], value:thesplit[1]});
			
	}
	while (result != null);

Everything works, the problem is I am getting a null exception at the end of the loop. I have a feeling the "while not null" condition is inherently flawed, but I can't think of any sort of limiting condition. The real problem is that the Regex object doesn't tell you how many matches it can find. It just gives you the first one and you have to keep calling it until it coughs up an error.

It's really strange and I've seen it done almost identically on other sites without it fucking up. I think, perhaps, I am missing something critical.

TIA

*edit* note that doing a try catch on the assignment of result will cause a very delightful endless loop.

*edit2*

I ended up having to write the do block like this:
var theres = null;
		
		try
		{
			theres = rgx.exec(hashstring["trail"])[0];
		}
		catch(e){}
		
		if(theres != null)					
			result = theres;
		else
			result = null;
		
		if(result != null)
		{
			if(result.substring(0,1) == ":")
			{
				result = result.substring(1);
			}
			
			if(result.substring((result.length - 1), result.length) == ":")
			{
				result = result.substring(0, (result.length - 1));
			}
			
			result = result.replace(",", "");
			
			var thesplit = result.split(":");
				
			hs.push({key:thesplit[0], value:thesplit[1]});
		}

For some reason assigning straight to result with try/catch caused it to flip the fuck out, so I used an intermediary variable. This just seems wrong. I'm looking for a better way to write this.

this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

we also talk about other random shit and clown upon each other
Jasconius on

Posts

  • JHunzJHunz Registered User regular
    edited June 2009
    rgx.test() will tell you if there is a match in the subject string. You can also do it explicitly (but more cleanly than your current try/catch stuff) with the following:
    resultArr = rgx.exec(hashstring["trail"]);
    if (resultArr == null)
        result = null;
    else
        result = resultArr[0];
    
    The problem with your current code is that rgx.exec returns an array, which you are accessing the first index of without checking if the array is non-null.

    JHunz on
    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
Sign In or Register to comment.