Vue/Lodash RegExp issue with results that contain ( ) or + [duplicate] - javascript

This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 2 years ago.
I've got a very simple script working that filters job opportunities by their name (see list image). This dropdown filters everything, except any opportunity name that has parentheses or a plus sign, and I'm not up to scratch enough to understand how I can effectively get around this issue without changing 102,000 rows in a database.
This is the snippet of code that performs the RegExp match (it's working fine, aside from those characters)
return _.orderBy(
this.results.filter(result =>
result.opportunity.match(
RegExp(this.opportunitySearch, "i")
)
)
);
And as you can see here, this is how Vue is receiving it:
Is anyone able to help point me in the right direction please?

Escape your regex. Those 3 characters need to be escaped or will be interpreted as meta characters.
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
return _.orderBy(
this.results.filter(result =>
result.opportunity.match(
RegExp(escapeRegExp(this.opportunitySearch), "i")
)
)
);

Related

Javascript regex that escapes \ sign [duplicate]

This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)

Symbols inside values breaking if statements in PHP [duplicate]

This question already has answers here:
AJAX POST and Plus Sign ( + ) -- How to Encode?
(6 answers)
Closed 5 years ago.
I have a web app where if someone selects something in the dropdown menu, it changes the next field with Ajax. I'm having difficulty when the values of the dropdown have a '+' symbol which breaks it.
For example this works:
if ($_GET['ch'] == 'Something here - here') {}
However this does not
if ($_GET['ch'] == 'Something here + here') {}
I'd like a solution to be able to include the + symbol inside. Some symbols seem to work fine including brackets (), dashes -, etc.
Try encodeURI function, and/or use POST instead.
Also escaping characters would be good. (like \+ instead of +)
When you are escaping characters than at php side you should use stripslashes function if you need special characters.

Javascript Regex not working while used in a angular controller [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Match exact string
(3 answers)
Closed 5 years ago.
I have written the following function in javascript to validate a string that I will use as a file name. I would like to check for all the characters that are restricted by Windows OS as invalid while creating files. I checked the regular expression at RegExr, it seems to be working as expected but it doesn't work when called from an Angular controller and it only matches the first character in the parameter. I'm adding the file extension later on so that isn't a problem.
Can anybody help with it? I'm relatively new to regular expressions and would appreciate any help or pointers to useful resources.
function validateInput(value) {
if (!AngularUtils.isUndefinedOrNull(value)) {
var regex = new RegExp("[^<>/\\\\=|:*?\"]+$");
var regexOutput = regex.test(value);
if (!regex.test(value))
return true;
}
return false;
}
Edit:
Even after changing the regex to handle javascript constructors, I'm still getting valid matches for the following input: "sample_css", "sample=css","=sample"
Only the first string should be valid. jsfiddle here.

Regex. Escape group? [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 5 years ago.
Is there any way to make something.+()[]* matching literally 'something.+()[]*'? I'm using regex builder so manual escaping is not allowed. Sure, i can add hardcoded checks if (char === '+') return '\+' but i'm looking for native solution or better way
UPD
I'm sorry. I forgot to add that matching should be in given order with moving forward but not back. So [+.] will not fit my requirements because it will match both +. and .+. I need only first case (In definition order)
You don't need to escape them if within square brackets.. I just tested and works for me, but maybe not what you are looking for?
something[.+()[]]

Regex to restrict certain characters [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
is there someone that can help me come up with a regex that restricts these characters !##$%^&*()_-? Most of the post here and google lead me to regex that allow a prescribed set of characters and I cannot seem to have much luck coming up with my own.
Thanks in advance.
EDIT:
After some tries came up with below and seems to work so no further help needed:
"Has restricted-characters_?".replace(/^\!|\#|\#|\$|\%|\^|\&|\*|\‌​(|\)|\_|\-|`|\?/, " ")
rgx = (/[\^!##$%^&*()_?-]/g);
test = str => console.log(str ,str.match(rgx))
test('qwerty')
test('!qwerty')
test('q#werty')
test('qw#erty')
test('qwer$ty')
test('qwert%y')
test('qwerty^')
test('qwertyq&werty')
test('qwertyqwe*rty')
test('qwertyqwert(y')
test('qwertyqwerty)')
test('qwertyqwertyq_wertyqwerty')
test('qwertyqwertyqwe?rtyqwerty')
test('qwertyqwertyqwert-yqwerty')
[^!##$%^&*()_?-]
will do for all the characters. You've got to put them in [ ] with a ^ in front.
Note to escape the - with a \ (Ex: [^!##$%^&*\-()_?]) or put the - in the very end.
This works, you can try it in here,
https://regexr.com/

Categories

Resources