0

I need some help with a JS Regex.

Here's the string I'm passing, I want to delete everything before 'Hanyuu-sama' with JS Replace.

Hanyuu","dj":{"id":18,"djname":"Hanyuu-sama

The first and second "Hanyuu" can change, the id number can change. This has already been cropped quite a bit with regular expressions.

Now I've tried a few and surprisingly it's failing when I do simple and complex regexes:

I've tried:

.*\"

And it does nothing, I've tried disgusting stuff in my desperation:

.*\","dj\":{\"id":.*,\"djname\":\"

And nada.

Here's a JS Fiddle and here's a http://regex101.com/r/tE2uY0/1 Regex JS matching platform.

Does anyone know why this isn't working?

I know this is likely bad practice, I'm just trying to learn Regexes.

Bonus points if anyone can refer me to a good source to learn Regular expressions. I'd love a solution but I'd like to learn how to do this myself in the future and why this one failed even more.

3
  • You're not using a regex in your demo. You're passing a simple string. Commented Aug 10, 2014 at 21:53
  • Can you further elaborate? I think this line is a regex: source = source.replace(".*\"", ""); Commented Aug 10, 2014 at 21:56
  • 2
    In JavaScript, a regular expression literal is denoted with a / character leading and trailing the pattern, and optionally modifiers after the trailing /. You have " characters instead of /, which means you've denoted a string literal instead of a regex literal. A regex literal would be /.*"/. Commented Aug 10, 2014 at 21:58

1 Answer 1

2

Your method call should look like this:

source = source.replace(/.*"/, "");

Regular expression in javascript are written between /.../ and not "/.../" like they are in many other languages.

If your string is always structured like that and it does not contain any more characters, your regex should do the trick. That's because the * quantifier acts greedy by default, thus always matching the last " in the string.

Sign up to request clarification or add additional context in comments.

1 Comment

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.