-1

How do I search a string for multiple periods in a row, for example "...". string.search(/.../); string.search("[...]"); The above will return true even if a single period is detected. string.search("..."); This is even worse as it always returns true, even if there is no period.

    var banlist = ["item1", "item2", "[...]"];

I am searching each string in the list, and I need to be able to search for "...".

Edit:

Now I have a new problem. three periods exactly "..." comes out to unicode character 8230, and it is not found in the search. Any other sequence of periods can be found except for three "...".

www
  • 1
  • 1

2 Answers2

0

You need to escape the . character using a backslash \

let str = "som.e ra...ndom s.tring..";
console.log(str.match(/\.{2,}/g));

where {2,} indicates that there has to be at least 2 . characters in a row and /g indicates that the whole string should be evaluated. Notice that the . is escaped.

Julian
  • 1,277
  • 1
  • 9
  • 20
0

Use \\ to escape the dot in your search.

For example '\\.\\.' or '\\.{2}'

string.search() is useful for getting the index of the first occurrence of a pattern. It returns -1 if pattern isn't found.

string.search('\\.{2}')

With string.search going through the list would be something like

for (let i = 0; i < banlist.length; i++) {
    if (banlist[i].search('\\.{2}') != -1) {
        // Do something
    }
}

If you don't need the index and just want to know if there are 2 or more dots in the string, string.match might be useful since it returns null when the pattern doesn't match.

string.match('\\.{2}')

With string.match going through the list would be something like

for (let i = 0; i < banlist.length; i++) {
    if (banlist[i].match('\\.{2}')) {
        // Do something
    }
}
Toivo Mattila
  • 377
  • 1
  • 9