gw1500se Posted November 18, 2022 Share Posted November 18, 2022 I have an array of regular expressions that I want to check against a string. I need to know if the string matches any of those regexps. Is there a way to do that without using a loop? TIA. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2022 Share Posted November 18, 2022 Yes, of course there is. If your array contains, say, 10 expression then you can write out 10 if() expressions - 1 for each array element). For example if (preg_match($regex[0], $mystr)) { // do something } elseif (preg_match($regex[1], $mystr)) { // do something else } elseif (preg_match($regex[2], $mystr)) { // do something else } ... //etc Quote Link to comment Share on other sites More sharing options...
requinix Posted November 18, 2022 Share Posted November 18, 2022 Well there has to be a loop somewhere, question is whether you write it yourself or not. Array.some const regexes = [/a/, /b/, /c/, /d/, /e/]; const input = "something"; const any = regexes.some(re => re.test(input)); Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 18, 2022 Author Share Posted November 18, 2022 1 hour ago, requinix said: Well there has to be a loop somewhere, question is whether you write it yourself or not. Array.some const regexes = [/a/, /b/, /c/, /d/, /e/]; const input = "something"; const any = regexes.some(re => re.test(input)); I don't want to write the loop myself if I don't have to which was what I was trying to say. I don't think I understand what you did. First don't you need to run 'regexes' through the 'RegExp' function? Then is 'test' a function of 'some'? I was not able to follow the link you provided as it relates to this question. I assume from the documentation that 'any' will be either 'true' or 'false', right? Quote Link to comment Share on other sites More sharing options...
requinix Posted November 18, 2022 Share Posted November 18, 2022 2 hours ago, gw1500se said: I don't want to write the loop myself if I don't have to which was what I was trying to say. I don't think I understand what you did. First don't you need to run 'regexes' through the 'RegExp' function? Then is 'test' a function of 'some'? I was not able to follow the link you provided as it relates to this question. I assume from the documentation that 'any' will be either 'true' or 'false', right? /.../ is a RegExp. You only need to deal with the class if you want to create a regex from a string. And it's also not actually a function but a class, however you can invoke it like a function and get an instance in return. .test is a function on re, which is one of the regular expressions in the array. The whole point of Array.some is that it executes some function for every item in the array until it finds one that passes (the function returned truthy), in which case it returns true itself. "Are there some items in the array which meet some criteria?" Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 19, 2022 Author Share Posted November 19, 2022 (edited) This is not working as expected. Perhaps I am missing something. First I do this: regexes=[] for (let item in config_data.excludes) { regexes.push(new RegExp(item)); } 'config_data.excludes' is an array of strings representing regexps. Then I do this in a loop: if (regexes.some(re=>re.test(item.title))) { console.log("skipping "+item.title) continue; } This block is in a loop (item.title) so the 'continue' will skip to the next item in the loop. Unfortunately, it finds the 'if' is always true although none of the items contain any of the rexexps. I didn't let it run long enough to actually encounter a title that does match but is the return boolean backwards from what I expected? Edited November 19, 2022 by gw1500se Quote Link to comment Share on other sites More sharing options...
requinix Posted November 19, 2022 Share Posted November 19, 2022 What are the exact contents of config_data.excludes and item.title? Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 20, 2022 Author Share Posted November 20, 2022 For testing, config_data.excludes has one entry: Array(1) 0: "/FRENCH*/" length: 1 items.title has many entries but these are typical: skipping Pick a Youtube thumbnail/title (~10 seconds) skipping Survey for $0.25 (~4 Minutes)(~ 4 minutes) skipping 2-minute study for $0.30(~ 2 minutes) skipping 2023 Plans As you can see the code is "skipping" even though 'FRENCH*' does not exist in those strings. Quote Link to comment Share on other sites More sharing options...
maxxd Posted November 20, 2022 Share Posted November 20, 2022 This worked in my testing: let regexps = [ new RegExp('test', 'gi'), new RegExp('another', 'gi'), new RegExp('more', 'gi'), ]; let entries = [ 'More string', 'Second string', 'Super string', 'Basic string', ]; // loop through the RegExps let res = regexps.map(re => { // then loop through the strings return entries.map(itm => { return re.test(itm); }) // flatten the resulting arrays and pipe that to some() }).flat().some(e => { return e == true; }); console.log(res); Quote Link to comment Share on other sites More sharing options...
requinix Posted November 20, 2022 Share Posted November 20, 2022 Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 20, 2022 Author Share Posted November 20, 2022 ????? Quote Link to comment Share on other sites More sharing options...
requinix Posted November 20, 2022 Share Posted November 20, 2022 I guess I'm saying you've got three solutions in this thread that all work, so if whatever you're doing isn't, you've got a different problem. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 21, 2022 Author Share Posted November 21, 2022 (edited) I think the problem is here: for (let item in config_data.excludes) { regexes.push(new RegExp(item)); } The result in regexexs is one entry and it is '/0/' because 'item' is '0' rather than '/FRENCH*/'. Edited November 21, 2022 by gw1500se Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 21, 2022 Author Share Posted November 21, 2022 Got it. for (var i=0; i<config_data.excludes.length; i++) { regexes.push(new RegExp(config_data.excludes[i])); } I always struggle with data types in Java Quote Link to comment Share on other sites More sharing options...
kicken Posted November 21, 2022 Share Posted November 21, 2022 1 hour ago, gw1500se said: I think the problem is here: for (let item in config_data.excludes) { regexes.push(new RegExp(item)); } The result in regexexs is one entry and it is '/0/' because 'item' is '0' rather than '/FRENCH*/'. You need to use of not in when doing the equivalent of foreach in php. for (let item of config_data.excludes) { regexes.push(new RegExp(item)); } in loops over the properties of an object, not the values of an iterable. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 22, 2022 Author Share Posted November 22, 2022 I thought this would be easy but I am really bad at regexps. I want to match the string 'FRENCH' anywhere in a string. /FRENCH/ Does not work. Please help. TIA. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 22, 2022 Share Posted November 22, 2022 Merged. 2 hours ago, gw1500se said: I thought this would be easy but I am really bad at regexps. I want to match the string 'FRENCH' anywhere in a string. /FRENCH/ I lost track of whether this was pointed out, let alone resolved, but are your regular expression strings including the / delimiters too? Because they should not do that. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 22, 2022 Author Share Posted November 22, 2022 Not sure how this became part of the previous topic although it is related in in that it is applying that solution. I thought I started a new topic. What I posted was the string before running it through Javascript RegExp. I assumed that the string was supposed to be a regexp expression string that RegExp would convert. Are you saying that the string should just be: FRENCH Quote Link to comment Share on other sites More sharing options...
requinix Posted November 23, 2022 Share Posted November 23, 2022 You did start a new topic. But given how it's very closely related to this one, and how you didn't really include any information besides "I TRIED /FRENCH/ AND IT ISN'T WORKING HALP", I figured I''d save everyone (including myself) the headache of having to pull some more teeth by just bringing everyone back in here. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 23, 2022 Author Share Posted November 23, 2022 I thought a new thread would be better, sorry. In any case I am confused as I thought a regexp needed the '/' delimiters. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 23, 2022 Share Posted November 23, 2022 48 minutes ago, gw1500se said: I thought a new thread would be better, sorry. In any case I am confused as I thought a regexp needed the '/' delimiters. The / are part of the traditional syntax for it. They don't actually contribute anything to the regular expression itself, but they can be useful to separate the expression part from any flags you might want to apply. Since Javascript's RegExp separates the expression from the flags, and since it needs the expression as a string, there's no point in requiring the slashes. But if you write a regex literal, so not using the RegExp class directly, then you have to use the slashes because (a) that's the syntax so Javascript knows it's a regular expression and (b) you also have to specify flags at the same time and the slashes can separate them. /foo/i <=> RegExp("foo", "i") https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp Quote Link to comment Share on other sites More sharing options...
gw1500se Posted November 23, 2022 Author Share Posted November 23, 2022 Got it. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.