Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Advanced String Searching

    Hi everyone,

    I'm trying to create a script that will search for string occurrences. However, I want do it in such a way that I do not necessarily know what string I'm looking for as long as it is within two defined strings.

    For example, say I have the following string
    --------------------------------------------
    'name="firstname" name="lastname" age="22" name="middlename"'

    I want to be able to get all the strings between name="" i.e. firstname, lastname, and middlename into an array.

    Can anyone show me how I can script this? All I need is a guide of the process as I can do the coding. Better still if there's some readymade code, that would help as well.

    Thanks.

  • #2
    New Coder
    Join Date
    Dec 2004
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have one solution in Perl that is amazingly simple but my translation to JS is such that I can't get it to work in a similar manner.
    Code:
    $string = 'name="firstname" name="lastname" age="22" name="middlename"';
    @anams=($string =~ m/name\s*=\s*\"([^\"]+)\"/ig);
    print qq~@anams\n~;
    I also have a solution in JS that works despite it being ugly, it gets the job done.
    Code:
    <script type="text/javascript">
    
    myText = 'name="firstname" name="lastname" age="22" name="middlename"';
    var my_array = myText.split(" ");
    var a = new Array(my_array.length)
    
    for (var i=0; i < my_array.length; i++){
    thisword =my_array[i];
    if(thisword.match(/name\s*=\s*\"([^\"]+)\"/ig)){
    finalresult = thisword.replace(/name\s*=\s*\"([^\"]+)\"/ig,"$1");
    a[i]=finalresult+"\n";
    }
    }
    s=a.join("");
     
    alert(s)
    </script>
    -Bill
    Last edited by cyber11; 01-24-2006 at 09:05 PM.

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    here's how i'd do it

    Code:
    var text = 'name="firstname" name="lastname" age="22" name="middlename"';
    var m;
    var matches = [];
    do {
    	m = /name\s*=\s*"([^"]+)"/.exec(text);
    	if ( m ) {
    		text = text.substring(m.index+m[0].length);
    		if ( m[1] ) {
    			matches[matches.length] = m[1];
    		}
    	}
    } while ( m );
    for ( var i = 0; i < matches.length; i++ ) {
    	alert("matches[" + i + "] = '" + matches[i] + "'");
    }

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would do it like this:
    Code:
    function getNames(mytext)
    {
    var checkText = mytext.toString();
    var firstSplit = checkText.split(' ');
    var secondSplit = new Array();
    var nameArray = new Array();
    var i;
    var j = 0;
    
    for(i=0;i<firstSplit.length;i++)
    {
    secondSplit[i] = firstSplit[i].split('=');
    if(secondSplit[i][0] == 'name')
    {
    nameArray[j] = secondSplit[i][1];
    j++;
    }
    }
    alert(nameArray);
    }
    Then just send it the text that you want to edit.

    getNames('name=firstname name=lastname age=22 name=middlename')

    Or remove the argument from the function and just hard code in a value for the text:

    Code:
    function getNames()
    {
    var mytext = 'name=firstname name=lastname age=22 name=middlename';
    var firstSplit = mytext.split(' ');
    var secondSplit = new Array();
    var nameArray = new Array();
    var i;
    var j = 0;
    
    for(i=0;i<firstSplit.length;i++)
    {
    secondSplit[i] = firstSplit[i].split('=');
    if(secondSplit[i][0] == 'name')
    {
    nameArray[j] = secondSplit[i][1];
    j++;
    }
    }
    alert(nameArray);
    }
    Then just call it like this:

    getNames()

    Of course, you can keep in the double quotation marks, but I just took them out for testing.
    Last edited by konithomimo; 01-25-2006 at 07:13 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •