1

I'm trying to convert my Wordpress site to be entirely front-end, REST API driven. I'm getting rid of all of my php and pretty much using Wordpress as a CMS with no php requests. I have a lot of code that Wordpress was parsing for me, but I'll need to handle myself now.

I need to be able to split this into its parts: [my_social artist = "blink-182" facebook = "https://en-gb.facebook.com/blink182/" soundcloud = "https://soundcloud.com/blink-182" spotify = "https://open.spotify.com/artist/6FBDaR13swtiWwGhX1WQsP"]

I'd eventually like the output to be something like:

{
    tag: "my_social",
    artist: "blink-182",
    facebook: "https://en-gb.facebook.com/blink182/",
    soundcloud: "https://soundcloud.com/blink-182",
    spotify: "https://open.spotify.com/artist/6FBDaR13swtiWwGhX1WQsP"
}

I tried my hands at a regex, but wasn't too sure how to repeat any non-specific key-value groups. Any help?

1 Answer 1

1

You can use this regex to capture the content you want:

(\w+)\s*=\s*(".*?")|(\w+)

Working demo

And then you can create a new string using the captured content with:

"\1":\2,

And also concatenate the string:

"tag":"\3"
Sign up to request clarification or add additional context in comments.

3 Comments

Nice. I've never seen that .*? syntax before, but that really comes in handy. Thanks!
@user1771700 that is to transform a greedy operator into a lazy (or ungreedy) operator. It's quit useful to match the minimum match between boundaries
Yeah it definitely is very useful. I use the .* syntax in most of my operators, but didn't realize you could make it lazy like that. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.