Reputation: 9
I have the loop below.
I need to pull all the names (lskey
) that does not have include the letter p
in it, but my attempt is not working.
for(var i = 0; i < localStorage.length; i++) {
var lsKey = localStorage.key(i);
var lsValue = localStorage.getItem(localStorage.key(i));
if(lsKey.match("^p")) {
$("#appendedLS").append("<input type='hidden' name='" + lsKey + "' value='" + lsValue + "'/>");
}
}
If any one has answer, I would appreciate it.
Upvotes: 0
Views: 72
Reputation: 9
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
if(lsKey.indexOf("p")==-1){
//statement}
Upvotes: 0
Reputation: 20909
So close! Inside of a character class the ^
means negation, but outside it means "beginning of string".
The following should work:
if(lsKey.match("p") === null){
Just check for the character p
, if it doesn't exist then match()
will return null
.
That said, if you don't need to use regular expressions then it's much simpler to use the indexOf
method instead:
if(lsKey.indexOf("p") == -1){
Upvotes: 3