#fedihelp #javascript #regex #regularexpressions
someone please help me understand how this /regex/d bit of code works (links to a better resource than f'ing w3s also satisfactory).
w3schools.com/js/tryit.asp?fil…
why does it not return, for example, "aaaabb"?
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browserwww.w3schools.com
no type
in reply to benda • • •you're specifically asking it to catch two a's (aa) followed by two b's (bb).
if you want it to catch an arbitrary number of a's followed by an arbitrary number of b's, you have to do (a+)(b+)
the + means one or more.
benda
in reply to no type • • •Oriel Jutty
in reply to benda • • •@notype
developer.mozilla.org/en-US/do…
It returns the first match (which is
aabb
) and its capturing groups, which in this case are(aa)
(matching "aa") and(bb)
(matching "bb").String.prototype.match() - JavaScript | MDN
MDN Web Docsbenda
in reply to Oriel Jutty • • •no type
in reply to benda • • •@barubary to be honest, i'm not sure why they're trying to teach you to use the d modifier. it's not beginner stuff. i haven't ever used it.
developer.mozilla.org/en-US/do…
RegExp.prototype.hasIndices - JavaScript | MDN
MDN Web Docsbenda
in reply to no type • • •Kagan MacTane (he/him)
in reply to benda • • •@notype @barubary I came at this a different way, and wondered what the heck the /d modifier is doing. Turns out:
1) W3schools is completely wrong about what it does (shocker, I know 🙄); and
2) All it does is add a particular extra bit of information to the return value.
I'm getting my info from stackoverflow.com/a/73947884, to which I am greatly indebted, and major props to SO user Andrejs. I reproduced their work with the text and RE under consideration here. 1/2
Kagan MacTane (he/him)
in reply to Kagan MacTane (he/him) • • •@notype @barubary
I set txt to 'aaaabb', then ran these. Note that the only difference in output is the presence of an "indices" array when /d is used, and its absence otherwise. That is all.
I've been using JS professionally for 20 years now and never needed anything like /d. You can safely ignore it. 2/2
benda
in reply to Kagan MacTane (he/him) • • •no type
in reply to Kagan MacTane (he/him) • • •benda
in reply to no type • • •ive been learning from a damn robot. ugh. i just need to grab a for dummies book or something. internet searching is fine for specific answers but i need some sort of syllabus to go off. i got problems staying focused without one.
benda
in reply to benda • • •i learned html and css from a billboard.
no type
in reply to benda • • •@kagan @barubary then you can read pretty much any old used o'reilly book. the important parts of regex haven't changed in 15 years or more.
but one thing is for sure: knowledge of regex will get you places. it's everywhere.
benda
in reply to no type • • •no type
in reply to benda • • •i got my regex introduction from the perl book. simple, no-nonsense: perldoc.perl.org/perlre
here's another classic book, though more hefty: regex.info/book.html
whatever you do with these old documents, skip over the software specifics (which are outdated) & go directly to the regex syntax (which has barely changed since).
perlre - Perl regular expressions - Perldoc Browser
perldoc.perl.orgbenda
in reply to no type • • •no type
in reply to benda • • •benda
in reply to no type • • •no type
in reply to no type • • •benda
in reply to no type • • •no type
in reply to benda • • •benda
in reply to no type • • •this is me learning programming like i promised you i would... over a year ago? i keep my promises but my timeframes are always whacked lol