0

I'm cleaning up filenames, e.g.

from

zx5-565x372.jpg?642e0d

to

zx5-565x372.jpg

Specifically, I want to remove the ? followed by 6 lowercase alphanumeric characters.

I've tried regex like

modified = original.replace("\?\w{6}", "") 

where \w is same as [a-zA-Z0-9_] and {6} is 6 of the same but with no joy.

Could someone kindly show me the right way?

1
  • why not just something like that: \?(.*) Commented May 10, 2015 at 13:24

2 Answers 2

1

You are using a string, not a RegExp.

var modified = original.replace(/\?\w{6}$/, "");
Sign up to request clarification or add additional context in comments.

Comments

0
modified = original.replace("\?\w{6}", "")
                            \_______/

This is just a string literal, it's not matched as a regex pattern.
You are literally replacing the string ?w{6} with an empty string (because escaped ? and w have no special meaning).

Use regex literals instead:

modified = original.replace(/\?\w{6}/, "");

Or just loosen your regex requirements in case the format changes:

modified = original.replace(/\?.*/, "");

Comments

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.