Tricky RegEx Capture [duplicate] - javascript

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I've got a couple strings and I need to pull characters out that appear between double quotes. The problem is, I want to grab them in groups.
var str = 'FF\"J"A4"L"';
var results = str.match(/\"(.*)\"/);
This returns everything between the first and last double quote. In this case it returns J"A4"L but what I need it to return is J and L.
The content between quotes is pretty much any unicode character like letters and numbers including as }, =, and #.
Any ideas on how to complete this with regex?

It sounds like the content between quotes is any character except for a quote, in which case you can get away with
/"([^"]*)"/

what you're looking for is this with the /g "global flag":
/("[^"]*")/g
In your example, it's like this:
var str = 'FF\"J"A4"L"';
var results = str.match(/("[^"]*")/g);
When doing this, results would be [""J"", ""L""], which contains the entire match (which is why the extra quotes are there).
If you wanted just the matched groups (which returns just the groups, not the whole match area), you would use exec:
var str = 'FF\"J"A4"L"';
var results = []
var r = /("[^"]*")/g
match = r.exec(str);
while (match != null) {
results.push(match[1])
match = r.exec(str);
}
Now, results is ["J", "L"]

Related

Replacing only exact matches in Regex [duplicate]

This question already has answers here:
How can I match a whole word in JavaScript?
(4 answers)
Closed 4 months ago.
I have a javascript function that replaces strings that match against a profanity library with symbols.
eg. damn is transformed into ##$%&!
However, this replace is non-discriminating, and will transform partial matches as well.
eg. classic is transformed into cl##$%&!ic
I'm very new to TS/JS and Regex and would like some help understanding this function's arguments and how to modify them for only exact matches.
this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof (str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
To avoid partial matches, the normal solution is to surround what you want to match with word boundaries \b.
The following example assumes that profanities does not contain any words that contain regex special characters.
Notice that the "shit" in "mishit" and the "ass" in "class" do not get replaced.
const profanities = ['damn', 'shit', 'ass'];
// As regex literal: /\b(?:damn|shit|ass)\b/
const re = new RegExp('\\b(?:' + profanities.join('|') + ')\\b', 'gi');
const text = "Damn, another mishit. I am shit at pool. Time to get my ass to class.";
console.log(text.replace(re, '##$%&!'));

Re-replacing in regex [duplicate]

This question already has answers here:
Regex matching 5-digit substrings not enclosed with digits
(2 answers)
Closed 2 years ago.
I am creating a function that replaces the string with a
~(number)~.
Now let's say I have a string that says
This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2
I want to replace all the 2 in a string with ~86~ but when I am doing so the 2 in ~26~ and ~524~ also getting replaced to ~~86~6~ and ```~5~86~4~.
function replaceGameCoordinate() {
var string = `This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2`
var replaceArr = ['2'];
let patt = new RegExp(`${replaceArr[0]}`, 'gm')
var newString = string.replace(patt, "~86~");
console.log(newString);
}
replaceGameCoordinate();
The expected output should be :
This is the replacement of ~26~ and ~524~. We still have ~86~ cadets left. Have~86~go for the next mission.~86~
So you need a different regex rule. You don't want to replace 2. You want to replace 2 when it's not next to another number or ~.
In order to do this, you can use lookaheads and lookbehinds (although lookbehinds are not yet supported by regexes in JS, I believe, but at least with lookaheads) :
const input = "This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2";
const regex = /2(?![\d~])/gm // Means : "2 when it's not followed by a digit \d or a ~"
console.log( input.replace(regex, "~86~" ) )

How to preserve character case using string.replace()? [duplicate]

This question already has answers here:
Replace text but keep case
(11 answers)
Closed 2 years ago.
I am attempting to write a function that will replace all of the vowels in a string with another arbitrary vowel which is selected by an end user. So far, I have been able to write a function that will replace all of the vowels regardless of case, but I would like to be able to preserve the case during replace().
Here is an example of what I am doing right now.
var selectedVowel = "a";
var vowels = /[aeiouAEIOU]/gi;
function rep(string){
let newString = string.replace(vowels, selectedVowel);
return newString;
}
rep("FooBar Exe. unIt");
// returns "FaaBar axe. anat"
// Intended output should return "FaaBar Axe. anAt"
I have tried using Regular Expressions to modify the search criteria for replace() and selectedVowel, but I can't figure out how to use the right regex characters to achieve that end.
I have also looked into methods that use split() to replace the first letter of a word, but this method seems to be limited to the string's indexes, which are not known at the time of the function call.
Any suggestions?
String.prototype.replace() takes a function in place of the substitution string, which is called for each match.
You could write a function that checks each match and replaces it with selectedVowel as-is or uppercased, depending on the case of the matched string.
A little trick to check if a character is upper/lowercase is to compare it to the upper/lowercased version of itself, as in match === match.toUpperCase().
var selectedVowel = "a";
var vowels = /[aeiouAEIOU]/gi;
function rep(string){
return string.replace(vowels, match => {
if (match === match.toUpperCase()) {
return selectedVowel.toUpperCase()
}
return selectedVowel
});
}
console.log(rep("FooBar Exe. unIt")) //=> "FaaBar Axa. anAt"

javascript search string for sequential periods [duplicate]

This question already has answers here:
Regex to check consecutive occurrence of period symbol in username
(2 answers)
Closed 4 years ago.
How do I search a string for multiple periods in a row, for example "...".
string.search(/.../);
string.search("[...]");
The above will return true even if a single period is detected.
string.search("...");
This is even worse as it always returns true, even if there is no period.
var banlist = ["item1", "item2", "[...]"];
I am searching each string in the list, and I need to be able to search for "...".
Edit:
Now I have a new problem. three periods exactly "..." comes out to unicode character 8230, and it is not found in the search. Any other sequence of periods can be found except for three "...".
You need to escape the . character using a backslash \
let str = "som.e ra...ndom s.tring..";
console.log(str.match(/\.{2,}/g));
where {2,} indicates that there has to be at least 2 . characters in a row and /g indicates that the whole string should be evaluated. Notice that the . is escaped.
Use \\ to escape the dot in your search.
For example '\\.\\.' or '\\.{2}'
string.search() is useful for getting the index of the first occurrence of a pattern. It returns -1 if pattern isn't found.
string.search('\\.{2}')
With string.search going through the list would be something like
for (let i = 0; i < banlist.length; i++) {
if (banlist[i].search('\\.{2}') != -1) {
// Do something
}
}
If you don't need the index and just want to know if there are 2 or more dots in the string, string.match might be useful since it returns null when the pattern doesn't match.
string.match('\\.{2}')
With string.match going through the list would be something like
for (let i = 0; i < banlist.length; i++) {
if (banlist[i].match('\\.{2}')) {
// Do something
}
}

JavaScript RegExp matching group takes unwanted parentheses [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 8 years ago.
I got text like: do[A]and[B]
I want to extract all words that are wrapped by [ and ].
I am using the match method:
var text = "do[A]and[B]";
var regexp = /\[(\w+)\]/g;
var result = text.match(regexp);
So I am saying that I want to match all words wrapped by [ ], but only the wrapped part should be in group/memory. I keep getting the [ ] parentheses in result:
["[A]", "[B]"]
expected result is:
["A","B"]
I thought this is a piece of cake to do, but I must be missing something.
For this particular case you don't need capturing groups:
>>> "do[A]and[Bbbb]".match(/\w+(?=])/g);
["A", "Bbbb"]
will do.
In order to work with subpatterns, there is no easy shortcut.
Instead, you have to repeatedly execute the regex on the string and collect the subpattern you want. Something like this:
var text = "do[A]and[B]",
regexp = /\[(\w+)\]/g,
result = [], match;
while(match = regexp.exec(text)) {
result.push(match[1]);
}

Categories

Resources