Link to home
Start Free TrialLog in
Avatar of eMisc
eMisc

asked on

count number of results in a string search

Hello,
I have this string and I need to check how many times '<li>' is present please:

<li>aho</li><li>hao</li><li>loa</li><li>ola</li>

Open in new window


thank you
Avatar of karunamoorthy
karunamoorthy
Flag of India image

which long u r using, php or ....
Avatar of eMisc
eMisc

ASKER

Experts Exchange- Scripting Languages- JavaScript- Count Number Of Results In A String Search
Avatar of Tom Beck
Assuming the <li>s are inside a <ul>, then in javascript you could do this:
<body>
<ul id="ul1">
<li>aho</li><li>hao</li><li>loa</li><li>ola</li>
</ul>
<script type="text/javascript">
alert(document.getElementById("ul1").getElementsByTagName("li").length);
</script>
</body>

Open in new window

SOLUTION
Avatar of JT92677
JT92677
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eMisc

ASKER

thank you!
eMisc -- tommyboy figured you were doing this in javascript. Okay, if so there are some useful properties and methods in javascript that you might want to check out as alternative ways to deal with strings and javascript.

For example:

    mystr = "<li>aho</li><li>hao</li><li>loa</li><li>ola</li>";

You can get the length as    lenmystr = mystr.length;

Then use replace method with the global match switch to replace all occurrences in the string

    mystr.replace("<li>"/gm, "");   to replace globally all the "<li>" found with nothing.

Now  mystr.length; property will be the length of the string without all the "<li>" substrings.

and the number of matches = (lenmystr - mystr.length) / "<li>".length

Check out http://www.w3schools.com/jsref/jsref_obj_string.asp

The /g switch means look for all (global), the /m means multi-line.

Just FYI - I realize the question is closed, but someone else may benefit, who knows.