-1

I have some code it searchs only case-sensitive. I've tryed toUpperCase()/toLowerCase() methods.

<script>
import json from './json/data.json';

export default {
  name: 'app',
  data(){
    return {
      search: '',
      blogs: json.slice(0,10),
    }
  },

computed: {
    filteredBlogs: function(){
      return this.blogs.filter((blog)=>{

        return blog.title.match(this.search) ||
               blog.lineId.match(this.search);


      });
    }
  }

}
</script>

Have any one any idea search case-insensitive?

  • Hi, Welcome to StackOverflow aka. "SO" Glad to have you apart of the community! Did you try searching SO? [JavaScript case insensitive string comparison](https://stackoverflow.com/a/2140723/1896134) Also, Please visit our [How to Ask](https://stackoverflow.com/help/how-to-ask), as this will guide you in getting more support from us in the community. – JayRizzo Jul 13 '19 at 08:51

1 Answers1

0

Assuming that this.search is a string, when you pass it to match it is implicitly converted to a RegExp which is case-sensitive by default.

Try to explicitly create a regular expression from this.search with "i" option that provides case-insensitivity:

computed: {
  filteredBlogs: function(){
    return this.blogs.filter((blog)=>{

      const regex = new RegExp(this.search, 'i')

      return blog.title.match(regex) ||
        blog.lineId.match(regex);
    });
  }
}
antonku
  • 7,377
  • 2
  • 15
  • 21