Assign a value to a variable using regular expressions

In a JScrip type script, having:

var name = item.name_stem;

How could I do to:

var title = first part of name;
var artist = second part of name;

The separator between the first and second part of the name would be a hyphen (-).
Example: 01. Billie Jean - Michael Jackson.mp3

I think the regular expressions should be these:

(^[0-9\.\s]*)(.*)(\s-\s)(.*) Select \2
(^[0-9\.\s]*)(.*)(\s-\s)(.*) Select \4

var title = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$2");
var artist = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$4");
1 Like

Wow @Leo, that looks wonderful, thank you very much, I'll experiment!

One thing to keep in mind is that x.replace(...) only works when x is a string.

That's fine with item.name_stem but some methods return things like Path objects instead of strings.

If you run into trouble with those methods, an easy way to force the object to become a string is to add an empty string to it.

For example, item.path gives you a Path object, but item.path + "" will give you a string.

1 Like

I understood @Leo, but it is working very well, thanks once again!

@Leo, let me ask you something else. How can I make a button only enabled when there are selected audio files?

Normally I use @disablenosel:type=*.(mp3|wma|ogg)but there are always files that are left out.

I'm asking you about something like: @disablenosel:type=metadata == "audio"

Thank you so much

Instead of *.(mp3|wma|ogg) try grp:Music. That will include all the extensions in the Music file type group.

1 Like