Jump to content

Recommended Posts

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

 

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));

 

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?

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?"

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 by gw1500se

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.

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);

 

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 by gw1500se
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.

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.

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.