Explain Javascript RegExp Behaviour [duplicate] - javascript

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 1 year ago.
I have this simple code:
let arr = [
'bill-items',
'bills',
'customer-return-items',
'customer-returns'
]
let re = new RegExp('^b*')
arr.forEach((e) => {
console.log(`Matching ${e}: ` + re.test(e))
})
I expect two matches and two non-matches. Strangely I get four matches!
Check here: https://jsfiddle.net/kargirwar/gu7Lshnt/9/
What is happening?

Try removing the "*" as following code snippets
"*" matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
let arr = [
'bill-items',
'bills',
'customer-return-items',
'customer-returns',
'when-b-is-not-the-first-character',
'b',
'' //empty string
]
let re = new RegExp('^b')
arr.forEach((e) => {
console.log(`Matching ${e}: ` + re.test(e))
})
The above code will match any strings that starts with letter b

It's quite simple
^. matches the beginning of the string. Each of the four strings has a beginning
b* matches any number of b also zero
And so, the regex matches the biggest part of the string it can. This is ^b for the first two strings and just ^ for the others.
If you want to match only strings that begin with at least one b use /^b+/ as the + requires at least one occurrence.
BTW you can use for instance https://regex101.com/ to test your regex and visualize the matches.

Related

javascript remove a exact index character from the string [duplicate]

This question already has answers here:
Remove a character at a certain position in a string - javascript [duplicate]
(8 answers)
Closed 1 year ago.
let test = 'This is the test string';
console.log(test.substr(3));
console.log(test.slice(3));
console.log(test.substring(3));
Theese methods are removing first 3 character. But i want to remove only third character from the string.
The log has to be: ' Ths is the test string'
İf you help me i will be glad. All examples are giving from the substr, slice eg. eg. Are there any different methods?
First, get the first 3 chars, then add chars 4-end, connect those to get the desired result:
let test = 'This is the test string';
let res = test.substr(0, 2) + test.substr(3);
console.log(res);
Since substr uses the following parameters
substr(start, length)
Start The index of the first character to include in the returned substring.
Length Optional. The number of characters to extract.
If length is omitted, substr() extracts characters to the end of the string.
We can use test.substr(3) to get from the 3'th to the last char without specifying the length of the string
const test = 'This is the test string';
const result = test.slice(0, 2) + test.slice(3);
console.log(result);
You can achieve this by concatenating the two parts of the string, using .slice().
You can achieve it using substring method and concatenating the strings
str = "Delete me ";
function del_one_char(string , removeAt){
return string.substring(0, removeAt) + string.substring( removeAt + 1, string.length);
}
console.log(del_one_char(str , 2))
// result one character at 2nd position is deleted

Getting strings between two specific occurrences of specific characters in JS

I am working on the following code. How can I extract/get strings between to specific numbers of characters in an string like
lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png?
What I need is:
one-two-three
I am able to remove things after the 9 occurrence of the - but not sure how to remove things before the 6 occurrence of - as well
var str = "lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png"
console.log(str.split("-", 9).join("-"));
Array.prototype.splice can be used to split an array.
var str = "lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png"
let out = str.split("-", 9).splice(6).join("-")
console.log(out);

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
}
}

How to write (A or B or C) followed by X in JavaScript regex? [duplicate]

This question already has answers here:
Regex to extract substring, returning 2 results for some reason
(5 answers)
Closed 6 years ago.
I would like to match a string on the format:
"XYZ 1234.10"
//XYZ can be one of a relatively large, but defined, set of 3 uppercase letter combination.
//String must start with XYZ or other from allowed set
//1234 can be an integer of arbitrary length >= 0
//10 can be an arbitrary integer in the range 0-99, but must have string-length of 2. (eg. 00, 03, 82)
//String must end with .NN (dot, number, number) /\.\d\d$/
I have tried the following regex, but captures "XYZ" as well as "XYZ 1234.10":
var regex = /^(XYZ|ABC|QST) \d+\.\d\d$/
regex.exec("XYZ 1234.10")
// => ["XYZ 1234.10", "XYZ"] would like it to be ["XYZ 1234.10"]
I would like to get a match only on the full expression. I have not been able to find a way to express this with javascript regex.
Is this achievable at all? If so: how can it be done?
// => ["XYZ 1234.10", "XYZ"] would like it to be ["XYZ 1234.10"]
exec is giving you the overall match followed by the contents of the capture groups. If you don't want the group to capture, make it a non-capturing group:
var regex = /^(?:XYZ|ABC|QST) \d+\.\d\d$/;
// ------------^^
console.log(regex.exec("XYZ 1234.10"));
You are mixing match and capture group. See output with a non capturing group:
var regex = /^(?:XYZ|ABC|QST) \d+\.\d\d$/;
console.log(regex.exec("XYZ 1234.10"));

Ignore regex capturing group when using javascript split [duplicate]

This question already has answers here:
JavaScript string split by Regex results sub-strings include empty slices
(2 answers)
Closed 4 years ago.
I'm trying to split up a string into an array, and I'm looking to get back an array with the following format: ['a','b', 'c']
const code = "/*_ ex1.js */a/*_ ex2.js */b/*_ ex3.js */c"
code.split(/\/\*_.+?\*\//)
=> (This is what I want)
['a','b', 'c']
But when I try to ensure that the regex works with new lines
code.split(/\/\*_(.|\s)+?\*\//)
=>(Not what I want)
[' ', 'a', ' ', 'b', ' ', 'c']
I have no idea where these extra spaces are coming from. It obviously has something to do with the bracketed capturing group, but I don't understand how to get around that.
split includes the contents of any capturing group in the output. From MDN:
If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array.
In your case, the (.|\s) is a capturing group. Therefore, spaces are getting included in your output. The easiest way around this is to make it a non-capturing group with ?::
code.split(/\/\*_(?:.|\s)+?\*\//)
^^
This still leaves you with an initial empty string in the resulting array. (Your initial, non-multi-line version also behaves that way.) There is no way around that, since your splitter is coming right at the beginning of the string, and so the token to the left is an empty string. If you want to get rid of that, you could filter it out:
.filter(Boolean)
Try using String.prototype.match() with RegExp /[a-z](?=\/|\n|$)/g to match character class a through z followed by / character or newline character or end of input
const code = "/*_ ex1.js */a/*_ ex2.js */b/*_ ex3.js */c\n"
+ "/*_ ex4.js */d/*_ ex5.js */e/*_ ex6.js */f";
var res = code.match(/[a-z](?=\/|\n|$)/g);
console.log(res);

Categories

Resources