General questions re Javascript in BRU

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

General questions re Javascript in BRU

Postby RegexNinja » Tue Mar 24, 2020 1:18 pm

Hi,
I'm trying to learn some javascript basics, and have a couple of general questions.
Please note: I'm using the portable free version of BRU, just trying to learn some basics to help others.

So far, I cant seem to input Positive or Negative LookBehinds like (?<=...) or (?<!...)
I figured it was probably the < character, so I tried preceding it with chars like \ to no effect.
I also tried single-quoting, double-quoting, and hex-notations like: \x3C and 0x3C, no effect.
Keep getting the error... 'Invalid Group'

Also, is there any way to prompt the user to input data?? I've been trying something like:
var userbrthdy = prompt("What is your birthday?");
Keep getting the error... 'prompt is not defined'

Is there another syntax for using prompt() within BRU?
Would editing sugar.js in any way, allow this to work?

One more question, kinda related to making prompt() work.
When creating some extra library.js files alongside with date/sugar.js...
Is there a way to make BRU auto-see them as it does with date/sugar.js?

I tried creating one, but its name didnt appear under Special / Javascript-Libraries.
No big deal, the manual (pg194) states the equivalent as: require('PathTo/MyLibrary.js');
http://www.bulkrenameutility.co.uk/foru ... =12&t=4743

If anyone has advice, tips, or tricks to make such things like this work, it would be much appreciated.
You'd be helping me to help others too.. So far, all my learning is either from the manual or researching online.
Always looking for good web-references to learn by (if we're allowed to post them on forum)?
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: General questions

Postby Admin » Wed Mar 25, 2020 8:26 am

Hi

So far, I cant seem to input Positive or Negative LookBehinds like (?<=...) or (?<!...)
I figured it was probably the < character, so I tried preceding it with chars like \ to no effect.
I also tried single-quoting, double-quoting, and hex-notations like: \x3C and 0x3C, no effect.
Keep getting the error... 'Invalid Group'


Is this for RegEx in Javascript?

Also, is there any way to prompt the user to input data?? I've been trying something like:
var userbrthdy = prompt("What is your birthday?");
Keep getting the error... 'prompt is not defined'


Pleaae note that javascript is run for every file , so the prompt would appear for every file which might be impractical atm.
This could be a new function in BRU

One more question, kinda related to making prompt() work.
When creating some extra library.js files alongside with date/sugar.js...
Is there a way to make BRU auto-see them as it does with date/sugar.js?
I tried creating one, but its name didnt appear under Special / Javascript-Libraries.
No big deal, the manual (pg194) states the equivalent as: require('PathTo/MyLibrary.js');


Yes, custom personal libraries can not be shown in the menu, only using require.

You'd be helping me to help others too.. So far, all my learning is either from the manual or researching online.
Always looking for good web-references to learn by (if we're allowed to post them on forum)?
Cheers!


Definetely!

I start :)
https://eloquentjavascript.net/

PS please note that most Javascript is supported in BRU but not ALL.
Also there are custom function and variables in BRU which are strictly BRU only, see help file or manual.
cheers
Admin
Site Admin
 
Posts: 1902
Joined: Tue Mar 08, 2005 8:39 pm

Re: General questions re Javascript in BRU

Postby RegexNinja » Wed Mar 25, 2020 10:24 am

Thanks for all of your replies Admin!

Yes I'm trying-out LookBehinds with javascript's .replace
Ex: Make Group1 match any TextString that does not terminate as xxx
newName = name.replace(/(.*)(?<!xxx)(.*)/, 'replacements')

I've got a feeling LookBehinds arent supported, since javascript only recently adopted them?
Even with BRU's regex, I have noticed some idiosynchracies between LookAheads & LookBehinds.
Luckily, there's workarounds in regex, but so far, not with LookBehinds in javascript.

I'm now beginning to understand why: 'javascript is run for every file'
At first I didnt agree with the logic.. Now of course, I see it's absolutely critical.
It makes perfect sense that variables like name/etc must be reset for each file!

I think it'd be a nice enhancement to specify global variables that dont reset per-file like counter.
(I'm gaining more and more insight into how BRU implements javascript.. Thanks!!)
Not sure if it'd be worthwhile.. Havent seen any requests, depends on how hard it'd be I guess.

Thanks for the link (fantastic), and all the info.. It'll take me some time, but I'll get there eventually.
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: General questions re Javascript in BRU

Postby Admin » Thu Mar 26, 2020 1:03 am

There is a nice chapter re Regular Expression in the book:

https://eloquentjavascript.net/09_regexp.html

It starts with :

'Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.
Jamie Zawinski


Yuan-Ma said, ‘When you cut against the grain of the wood, much strength is needed. When you program against the grain of the problem, much code is needed.’
Master Yuan-Ma, The Book of Programming


:)

Not sure about LookBehinds in BRU Javascript Regex however IF conditional statements and loops are supported so you could combine that with RegEx too, e.g. if strings ends with XYZ then computer regex etc

cheers
Admin
Site Admin
 
Posts: 1902
Joined: Tue Mar 08, 2005 8:39 pm

Re: General questions re Javascript in BRU

Postby Admin » Thu Mar 26, 2020 1:04 am

PS More global variables like counter would be useful I think!
Admin
Site Admin
 
Posts: 1902
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming


cron