Regex Match ALL Dynamic string words to data attribute - javascript

Is it possible to have a dynamically generated string
var string= "a,b,c";
and make a regex out of that that accepts any of the strings words as accepted matches?
regExApproved = new RegExp(string, 'ig');
So that we can search a data-attribute of an element and show it if it has any of the accepted matches in the attribute
$(this).attr("data-payment").match(regExApproved)
The problem im facing is the regex takes the whole string and makes that the only match case.
I need the regex to break the string down and accept ANY word in it as a match
Is this possible?
I do not want to use a forloop and make LOTS of different regex Matches for each word in the string but maybe I have to?

I suggest using
var regExApproved = new RegExp(string.split(",").map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join('|'), 'i');
Then, to check if a regex matches a string, it makes more sense to use RegExp#test method:
regExApproved.test($(this).attr("data-payment"))
Notes:
.split(",") - splits into comma-separated chunks
.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')) (or .map(function(x) return x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); })) - escapes the alternatives
.join('|') creates the final alternation pattern.
Edit
It seems that your string is an array of values. In that case, use
var string= ['visa','mastercard'];
var regExApproved = new RegExp(string.join('|'), 'i');
// If the items must be matched as whole words:
// var regExApproved = new RegExp('\\b(?:' + string.join('|') + ')\\b', 'i');
// If the array items contain special chars:
// var regExApproved = new RegExp(string.map(x => x.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join('|'), 'i');
console.log(regExApproved.test("There is MasterCard here"));

If your string is a list of comma-separated words, you can split it by comma then use a combination of Array methods to check if any word is matched inside the test string.
This is how should be your code:
string.split(",").map(s => new RegExp('\\b' + s + '\\b')).some(r => $(this).attr("data-payment").match(r))
Where we have:
string.split(",") to split the string by comma.
.map(s => new RegExp('\\b' + s + '\\b')) to return a relevant regex for each word in the string using word-boundary.
.some(r => $(this).attr("data-payment").match(r)) to check if our string matches any one of the created regexes.
Demo:
var string = "is,dog,car";
let str = "this is just a test";
if (string.split(",").map(s => new RegExp('\\b' + s + '\\b')).some(r => str.match(r))) {
console.log("Matched");
}

Related

Need to replace a string from a long string in javascript

I have a long string
Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
removable_str2 = 'ab#xyz.com;';
I need to have a replaced string which will have
resultant Final string should look like,
cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
I tried with
str3 = Full_str1.replace(new RegExp('(^|\\b)' +removable_str2, 'g'),"");
but it resulted in
cab#xyz.com;c-c.c_ab#xyz.com;
Here a soluce using two separated regex for each case :
the str to remove is at the start of the string
the str to remove is inside or at the end of the string
PS :
I couldn't perform it in one regex, because it would remove an extra ; in case of matching the string to remove inside of the global string.
const originalStr = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;ab#xyz.com;c_ab#xyz.com;';
const toRemove = 'ab#xyz.com;';
const epuredStr = originalStr
.replace(new RegExp(`^${toRemove}`, 'g'), '')
.replace(new RegExp(`;${toRemove}`, 'g'), ';');
console.log(epuredStr);
First, the dynamic part must be escaped, else, . will match any char but a line break char, and will match ab#xyz§com;, too.
Next, you need to match this only at the start of the string or after ;. So, you may use
var Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
var removable_str2 = 'ab#xyz.com;';
var rx = new RegExp("(^|;)" + removable_str2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "g");
console.log(Full_str1.replace(rx, "$1"));
// => cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
Replace "g" with "gi" for case insensitive matching.
See the regex demo. Note that (^|;) matches and captures into Group 1 start of string location (empty string) or ; and $1 in the replacement pattern restores this char in the result.
NOTE: If the pattern is known beforehand and you only want to handle ab#xyz.com; pattern, use a regex literal without escaping, Full_str1.replace(/(^|;)ab#xyz\.com;/g, "$1").
i don't find any particular description why you haven't tried like this it will give you desired result cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
const full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
const removable_str2 = 'ab#xyz.com;';
const result= full_str1.replace(removable_str2 , "");
console.log(result);

Regex remove underscores from a given string

I try to transform string using String replace method and regular expression. How can I remove underscores in a given string?
let string = 'court_order_state'
string = string.replace(/_([a-z])/g, (_, match) => match.toUpperCase())
console.log(string)
Expected result:
COURT ORDER STATE
You could use JavaScript replace function, passing as input:
/_/g as searchvalue parameter (the g modifier is used to perform a global match, i.e. find all matches rather than stopping after the first one);
(blank space) as newvalue parameter.
let string = 'court_order_state'
string = string.replace(/_/g, ' ').toUpperCase();
console.log(string);
In your code you could match either and underscore or the start of the string (?:_|^) to also match the first word and match 1+ times a-z using a quantifier [a-z]+
Then append a space after each call toUpperCase.
let string = 'court_order_state';
string = string.replace(/(?:_|^)([a-z]+)/g, (m, g1) => g1.toUpperCase() + " ");
console.log(string)
let string = 'court_order_____state'
string = string.replace(/_+/g, ' ').toUpperCase()
console.log(string)
It can be as simple as the below:
let string = 'court_order_state'
string = string.replace(/_/g, ' ').toUpperCase();
console.log(string);
Here the 'g' represents global, whereas the '/' is surrounded by what we're looking for.
Instead of matching the first character just after every _ and making them uppercase (from the regex that you have used), you can simply convert the entire string to uppercase, and replace the _ with space by the following:
let string = 'court_order_state';
string = string.toUpperCase().replace(/_+/g, " ");
console.log(string);

How can I ban a word

I want to replace a bad word with asterisks ***. However, there is a problem when the bad word is contained in an another word I don't want to replace it.
for(var i = 0; i < forbidden.length; i++) {
if(textBoxValue.search(forbidden[i]) > -1) {
textBoxValue = textBoxValue.replace(forbidden[i], '');
}
}
For example if the bad word is "are", if it is in another word like "aren't" I don't want it to appear as "***n't". I only want to replace the word if it is by itself.
One option is to use a regular expression with a word boundary on each side, to ensure that a matched word is standalone:
forbidden.forEach((word) => {
textBoxValue = textBoxValue.replace(new RegExp('\\b' + word + '\\b', 'g'), '');
});
For example:
let textBoxValue = 'bigwordfoo foo bar barbaz';
const forbidden = ['foo', 'bar'];
forbidden.forEach((word) => {
textBoxValue = textBoxValue.replace(new RegExp('\\b' + word + '\\b', 'g'), '');
});
console.log(textBoxValue);
If you actually want to replace with asterisks, and not the empty string, use a replacer function instead:
let textBoxValue = 'bigwordfoo foo bar barbaz';
const forbidden = ['foo', 'bar'];
forbidden.forEach((word) => {
textBoxValue = textBoxValue.replace(
new RegExp('\\b' + word + '\\b', 'g'),
word => '*'.repeat(word.length)
);
});
console.log(textBoxValue);
Of course, note that word restrictions are generally pretty easy to overcome by anyone who really wants to. Humans can almost always come up with ways to fool heuristics.
If any of the words to blacklist contain special characters in a regular expression, escape them first before passing to new RegExp:
const escape = s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
let textBoxValue = 'bigwordfoo foo ba$r ba$rbaz';
const forbidden = ['foo', 'ba$r'];
forbidden.forEach((word) => {
textBoxValue = textBoxValue.replace(
new RegExp('\\b' + escape(word) + '\\b', 'g'),
word => '*'.repeat(word.length)
);
});
console.log(textBoxValue);
You can create a dynamic regex with all the forbidden words separated by a | to create an alternation. You can wrap this with word boundary (\b) to replace only full word matches.
For the following list of forbidden words, the dynamic regex ends up being
/\b(?:bad|nasty|dreadful)\b/g
The second parameter to replace, gets the matched word as a parameter. You can use repeat to get * repeated the same number of times as the length of the word to be replaced
function replaceBadWords(textBoxValue, forbidden) {
const regex = new RegExp(`\\b(?:${forbidden.join('|')})\\b`, 'g')
return textBoxValue.replace(regex, m => "*".repeat(m.length))
}
const forbidden = ['bad', 'nasty', 'dreadful']
console.log(replaceBadWords('string with some nasty words in it', forbidden))
console.log(replaceBadWords("bad gets replaced with asterisks but badminton won't", forbidden))
If you're not yet using a library (Or if you want to use one)
You can check this repo out.
First, they already have a list of bad words so you don't need to think about them and think what you missed.
They support placeholders like:
var Filter = require('bad-words');
var customFilter = new Filter({ placeHolder: 'x'});
customFilter.clean('Don't be an ash0le'); //Don't be an xxxxxx
and you can add your own bad words like or remove it:
var filter = new Filter();
// add to list
filter.addWords('some', 'bad', 'word');
// remove from list
filter.removeWords('hells', 'sadist');
And also a multi lingual support if you have the correct regex.

Regular expression extract string

I have a string "#{Name; 11112121#xyz.com}"
I want to write a regular expression that extracts Name and 11112121 from the above string
This is what I tried.
function formatName(text){
var regex = /#\{([^;]+); ([^\}]+)\}/
return text.replace(
regex,
'$1, $2'
);
}
The above gives Name, 11112121#xyz.com. But I want only Name, 11112121
try this
var regex = /#\{([^;]+); ([^\}]+)(#.*)\}/
$1 => Name
$2=> 11112121
Here is the working example
Use match instead, like this:
var regex = /#\{([^;]+);\s+([^#]+)/;
var matches = text.match(regex);
alert(matches[1] + ', ' + matches[2]);
http://jsfiddle.net/rooseve/bM2U6/
If you want to match everything until the # character, you can use
var regex = /#\{([^;]+); ([^#]+)/
If you need to verify that the string also contains a } after that, you can add that to the end:
var regex = /#\{([^;]+); ([^#]+)[^}]*\}/

Regex Wildcard for Array Search

I have a json array that I currently search through by flipping a boolean flag:
for (var c=0; c<json.archives.length; c++) {
if ((json.archives[c].archive_num.toLowerCase().indexOf(query)>-1)){
inSearch = true;
} }
And I have been trying to create a wildcard regex search by using a special character '*' but I haven't been able to loop through the array with my wildcard.
So what I'm trying to accomplish is when query = '199*', replace the '*' with /[\w]/ and essentially search for 1990,1991,1992,1993,1994 + ... + 199a,199b, etc.
All my attempts turn literal and I end up searching '199/[\w]/'.
Any ideas on how to create a regex wildcard to search an array?
Thanks!
You should write something like this:
var query = '199*';
var queryPattern = query.replace(/\*/g, '\\w');
var queryRegex = new RegExp(queryPattern, 'i');
Next, to check each word:
if(json.archives[c].archive_num.match(queryRegex))
Notes:
Consider using ? instead of *, * usually stands for many letters, not one.
Note that we have to escape the backslash so it will create a valid string literal. The string '\w' is the same as the string w - the escape is ignored in this case.
You don't need delimiters (/.../) when creating a RegExp object from a string.
[\w] is the same as \w. Yeah, minor one.
You can avoid partial matching by using the pattern:
var queryPattern = '\\b' query.replace(/\*/g, '\\w') + '\\b';
Or, similarly:
var queryPattern = '^' query.replace(/\*/g, '\\w') + '$';
var qre = query.replace(/[^\w\s]/g, "\\$&") // escape special chars so they dont mess up the regex
.replace("\\*", "\\w"); // replace the now escaped * with '\w'
qre = new RegExp(qre, "i"); // create a regex object from the built string
if(json.archives[c].archive_num.match(qre)){
//...
}

Categories