javascriptregexmatchflags

RegExp matching part of a string


I've put together a search function that looks through lots of divs to match a string to information in its data attributes and I'm using regular expression to do this.

This is the regular expression I'm currently using which is working.

var regExPattern = "gi";
var regEx = new RegExp(filter, regExPattern);
// filter is the text that the user searches. 

I need to change the pattern so it can match part of a string e.g. it can match 'world' in 'hello world'. But at the moment mine only works if you type in the full string 'hello world'.

I had a read on the Mozilla page about regular expressions, and I think the flag I need to use is \S. So I changed my regExPattern to "gi/\S\w*/"...but this doesn't work. I think I'm going really wrong with how to use the flags. Please can someone point me in the right direction.


Solution

  • It turns out that it wasn't a problem with RegEx patterns or flags, as they were working as I'd hoped by searching through a whole string anyway, but it was a problem with my search function in total. Please see this later question I posted.