skip to Main Content

I’m moving a large blog from WordPress to another platform, and I am trying to re-create WordPress shortcodes with Javascript.
I’ve been able to write the Regex to replace [youtube]‘s shortcodes without problems, but I’m having problems with [soundcloud]‘s.

The format of the shortcode is the following:

[soundcloud url=”http://api.soundcloud.com/tracks/33660734”]

I created the following Regex rule, that seems to work on Regex 101

[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]
https://regex101.com/r/JKL45q/2

But when I include it in my script:

{
      service: 'soundcloud',
      regex: new RegExp('[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]', 'ig'),
},

I get this error, which I can’t understand:

main.js:29 Uncaught SyntaxError: Invalid regular expression: /[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]/: Unmatched ')' (at main.js:29:14)
    at new RegExp (<anonymous>)
    at main.js:29:14
    at main.js:142:3

The source script is here:
https://hotmc.pages.dev/assets/js/main.js

This is a page where the error occurs in the console:
https://hotmc.pages.dev/2011/01/10/esclusivo-anteprima-album-micha-soul-un-brano-in-free-download

Can anyone help? I found other questions about this error, but haven’t been able to apply the suggested fixes.

Thank you in advance,
S.

2

Answers


  1. Chosen as BEST ANSWER

    I have been able to get rid of the error by changing hot the RegExp object is initialized.

    From this:

    new RegExp('[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]', 'ig')

    To this:

    new RegExp(/[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]/, 'ig')


  2. I think you can remove the () of the ([a-zA-Z0-9]*)

    new RegExp('[soundcloud url="http(s?)://api.soundcloud.com/tracks/[a-zA-Z0-9]*"(s*?)]', 'ig').test(`[soundcloud url="http://api.soundcloud.com/tracks/33660734"]`)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search