1

I would like to extract word from tag like

<li>{firstname} {last name}</li>

So, can I get extract strings "firstname" and "last name" using javascipt or jquery?


Actually I think I have made some mistake to explain.

my String looks like

<li><a href="#contactDetail">{firstname} {lastname}</a></li>

here i would like to know how many parameters available means {first..} {last...} that can be only {fi..} and I would like replace with some other parameters.

So, I have a list of array which includes many parameters like firstname, lastname, address and so on... now I would like to replace that with according to what i will get from string.

Hopefully, I have tried my best to explain my problem.

2
  • What if someones name is Jan Willen van Boeken, where Jan Willen is his first name, van Broeken his last name. NO way to detect that... Commented Feb 14, 2011 at 13:11
  • 1
    The question is, are the curly brackets part of the string or not. Commented Feb 14, 2011 at 13:14

2 Answers 2

2

if the li element is part of the dom use something like:

var names = $('body ul li').text().split(' ');
var firstName = names[0];
var lastName = names[1];
Sign up to request clarification or add additional context in comments.

Comments

0

You could try something like this.

http://jsfiddle.net/W5Uh6/2/

Markup

<ol id="names">
    <li>Jason Brown</li>
    <li>Danny Glover</li>
    <li>Jack Jill</li>
</ol>

JS

$(function(){
    var firstNames = [];
    var lastNames = [];

    $("#names li").each(function(){
       var text = $(this).text().split(' ');

        firstNames.push(text[0]);
        lastNames.push(text[1]);
    }); 
});

Puts each word into an array that is separated by a space. You an then grab the values from the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.