Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript question - substring

Options
  • 17-11-2009 1:12pm
    #1
    Registered Users Posts: 1,127 ✭✭✭


    Given this string
    functionName('attributeA','attributeB');
    
    how would I extract 'attributeA' and 'attributeB' using indexOf and substring?

    Is it

    string.substring(string.indexOf("'"), ...)

    Im missing the ... bit, as Im not sure if there is a "nextIndexOf" method in javascript


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    I'm sure there are easier ways than this but try this code:
    	var testString = "functionName('attributeA','attributeB','attributeC');";
    	var args = new Array();
    	
    	function subMe() {
    		testString = testString.replace(/','/gi, " ");
    		testString = testString.replace(/', '/gi, " ");
    		testString = testString.replace(/'/gi, "");
    		testString = testString.replace(");", "");
    		args = testString.split(" ");
    		alert(args.length);
    		var i = 0;
    		while(i < args.length) {
    			if(i == 0) {
    				temp = args[i].split("(");
    				args[i] = temp[1];
    			}	
    			
    			document.getElementById('result').innerHTML += args[i] + "<br />";
    			i++;
    		}
    	}
    

    -RD

    P.S. : I didn't use indexOf or substring. If you require these then this is probably a homework assignment and with a bit of imagination the above code could be adapted to get the same result.

    -RD


Advertisement