4

I'm getting changing results from a method. It is everytime a String, but at the beginning of every String is written "String(xxx) "Text of String... ". The xxx depends on the number of characters inside of the String. I tried using regex to cut everything away before first appearance of " :

(result.replace(/^.+?(\") /, ''))

But I'm not sure if I used it correctly. I would also accept other approaches, which delete the beginning of the String in every case (independently of the length of the string).

Example:

return String: 'string(18) "This is an example"'

And I just want to get the String without the stuff at the beginning, so:

'This is an example'

But the return (and therefore the number in the parenthesis) can vary. I want to cut this out in every case.

4
  • You've put your space after the quote instead of before it var s = s.replace(/^.+? (\")/, '') Commented Oct 28, 2020 at 17:53
  • You'll also want to remove the " at the end .replace(/\"$/, "") Commented Oct 28, 2020 at 17:54
  • 2
    or split it on the quotation marks s.split('"')[1]; Commented Oct 28, 2020 at 18:03
  • @pilchard nice option, but it might need to handle quotes in the middle (OP doesn't indicate if these are possible) Commented Oct 28, 2020 at 18:08

2 Answers 2

1

You can achieve this with the following regex:

/.+\)\s\"(.*)\"/

As follows:

  • .+ - one or any number of characters
  • \)\s\" - until you reach ) "
  • (.*) - then capture everything
  • \" - until you reach the final "

Working Example:

const processString = (string) => {

  let processedString = string.replace(/.+\)\s\"(.*)\"/, '$1');

  console.log(processedString);
}


processString('string(18) "This is an example"');
processString('string(23) "This is another example"');
processString('string(25) "This is a "third" example"');

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

Comments

0

Use

string.replace(/^.*? "|"$/g, '')

See proof.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
   "                       ' "'
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

JavaScript code:

const string = 'string(18) "This is an example"';
console.log(string.replace(/^.*? "|"$/g, ''));

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.