Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript question - substring

  • 17-11-2009 01:12PM
    #1
    Registered Users, Registered Users 2 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