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:
thank you
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>
thank you
which long u r using, php or ....
ASKER
Experts Exchange- Scripting Languages- JavaScript- Count Number Of Results In A String Search
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
For example:
mystr = "<li>aho</li><li>hao</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.