2

Let's say I have the string:

var post = "[PostContent(postId=123e4567-e89b-12d3-a456-426655440000, postName=My New House, picture=house.jpg, location=San Francisco)]"

What I'm trying to rip out is the UUID from the postId so the new resulting string just has the value:

123e4567-e89b-12d3-a456-426655440000

Thanks for the help!

Update:

Sorry for the incomplete question I've already tried:

var firstvariable = "postId=";
var secondvariable = ", postName=";
string.match(new RegExp(firstvariable + "(.*)" + secondvariable));

and

string.substring(string.lastIndexOf("postId=")+1,string.lastIndexOf(", postName"));

and

var regex = /(.*postId=\s+)(.*)(\s+, postName.*)/;
var newtext = string.replace(regex, "$2");
3
  • Please share the code which you have tried. Commented Nov 29, 2017 at 18:41
  • Have you tried anything? Commented Nov 29, 2017 at 18:42
  • use postId=([\w-]+) regex Commented Nov 29, 2017 at 18:45

1 Answer 1

4

You can use String#match method.

var post = "[PostContent(postId=123e4567-e89b-12d3-a456-426655440000, postName=My New House, picture=house.jpg, location=San Francisco)]";

console.log(
  post.match(/postId=([\w-]+)/)[1]
)

Regex explanation here

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

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.