Ignore regex capturing group when using javascript split [duplicate] - javascript

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

Related

RegEx Split String and Leave Delimiter

I am trying to split a string and leave the delimiter intact. I am getting pretty close but I can't figure it out fully. I have found a lot of examples on Stack Overflow but I am still running in to an issue that I can't figure out. Here is the code:
"This is a string with ${G:VarSome:G} text".split(/\$\{(.+?(}\(.+?\)|}))/g)
The above code yields:
['This is a string with ', 'G:VarSome:G}', '}', ' text']
The result I am looking for is:
['This is a string with ', '${G:VarSome:G}', ' text']
The ${G:SomeVar:G} pattern is a templating system where variables will be injected. There are other formats of variables too for example ${G:AnotherVar:1}, ${G:DifferentVar:5} etc. After I get the split string, I will do a variable lookup in the system to inject the corresponding variable.
You are having an extra capture group inside the regex, this will give you a single group.
const result = "This is a string with ${G:VarSome:G} text".split(/(\${.+?})/g);
console.log(result);
The regex that graps a single capture group looks like this. it returns all the captured inside ()
/(\${.+?})/g
This should capture the examples provided. regex101
It's build with first getting
${ and then getting one or more characters lazily (.+?) until it encounters a }.
You can use this regex for splitting:
/(\${[^}]*})/
Code:
var s = 'This is a string with ${G:VarSome:G} text';
var arr = s.split(/(\${[^}]*})/);
console.log(arr);
Here:
\${[^}]*}: Will match ${ followed by 0 or more non-} characters followed by }

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

Explain Javascript RegExp Behaviour [duplicate]

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.

Why JS Regexp.exec returns an array with more elements than expected?

I'm attempting to regex match various duration strings (e.g. 1d10h, 30m, 90s, etc.) and have come up with a regex string to split the string into pieces, but it seems that I'm getting two undefined results at the ends that shouldn't be there. I imagine it has to do with the greedy matching via the ? groupings, but I'm not sure how to fix it.
My code looks like this:
const regex = /^(\d+?[d])?(\d+?[h])?(\d+[m])?(\d+[s])?$/gmi
const results = regex.exec('1d10h')
and the results I get look like so:
[
"1d10h",
"1d",
"10h",
undefined,
undefined,
]
I was only expecting the first three results (and in fact, I only really want 1d and 10h) but the two remaining undefined results keep popping up.
You have 4 groups in the regular expression - each enclosed with braces ( ... ) and enumerated naturally - the earlier opening brace appear in the expression the lower order index a group has.
And, of course, the whole match that could be named a "zero" group.
So, result of regex.exec('1d10h') contains 5 items:
results[0] - the whole expression match
results[i] - match of each group, i in {1,2,3,4}
Since in this case each group is optional (followed by ?) - it is allowed to have undefined in place of any unmatched group.
It is easy to see that if you remove a ? symbol after an unmatched group, the whole expression will fail to match and hence regex.exec('1d10h') will return null.
To get rid of undefined elements just filter them out:
const result = regex.exec('1d10h').filter(x => x);

match doesn't return capturing group [duplicate]

This question already has answers here:
Regex doesn't omit quotes when matching text in quotes
(3 answers)
Closed 7 years ago.
I'm trying to apply a regular expression to a string, in search of any placeholders, but I can't figure out why the result is only the full match, not the capturing group.
//----HTML------------------------//
<p>Let's try plaintext.<br/>
bommdidumpidudidoo</p>
<p id="output"></p>
//----JS------------------------//
var s = $('p').html();
var matches = s.match( /.*(plaintext)/g );
write(matches);
write(matches[0]);
write(matches[1]);
//------- whatever -------//
function write(s) {
$('#output').html( $('#output').html() +'<br/>'+s );
}
// output:
// Let's try plaintext
// Let's try plaintext
// undefined
» fiddle
(I've used my custom function write instead of console.log so that the result would show up in the fiddle)
Why is the third line undefined? I don't understand!
I'm pretty sure the expression is right. I'm 1oo% certain that this is the right capturing group syntax for JavaScript, and that .match() returns an array with the full match first, then all capturing groups. I've even tested it with regex101.com – over there, I do get the capturing group.
It's not the same as this other problem because there, the OR logic was the crux of the problem and here the pipe | doesn't even come up.
Oooh! It's because I'm doing a global search, with the g modifier. I do get the expected result if I remove that modifier. Tss.
I've used that modifier in the first place in order to grab multiple placeholders, but I guess I can still while-loop that stuff …

Categories

Resources