0

I need to replace substring in string.

String is /string/otherstring?query=d78f1dbee59de41245fbf3c82b72b859ab688e30

Substring query=d78f1dbee59de41245fbf3c82b72b859ab688e30 I need to replace to query=

I want to replace it using regexp and then using string.replace. But I can't write the regexp.

/\/string\/otherstring?query=/.exec(
    '/string/otherstring?query=d78f1dbee59de41245fbf3c82b72b859ab688e30'
)

It's not working.

P.S. d78f1dbee59de41245fbf3c82b72b859ab688e30 is sha1 hash

3
  • why do you need regex for this you can directly store this in string and you can replace using string.replace Commented Jul 17, 2015 at 7:38
  • you missed one \ near ? Commented Jul 17, 2015 at 7:38
  • "/string/otherstring?query=yoursha1hash".replace(/query=yoursh1hash$/, "query="); Commented Jul 17, 2015 at 7:42

2 Answers 2

1

Use string.replace . You don't need to apply the exec function.

string.replace(/(\/string\/otherstring\?query=)[A-Za-z0-9]+/g, "$1")

or

Try this if you want to deal with only lowercase letters and numbers.

string.replace(/(\/string\/otherstring\?query=)[a-z\d]+/g, "$1")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That's what I want
0

you could use like this in js

'/string/otherstring?query=d78f1dbee59de41245fbf3c82b72b859ab688e30'.replace(/.*query=/,'')

or

'/string/otherstring?query=d78f1dbee59de41245fbf3c82b72b859ab688e30'.replace(/\/string\/otherstring\?query=/,'')

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.