0

I receive from an api service this string

{\n "modelColor": ["000019000031262"]\n}"}

how can I display only the numbers : "000019000031262" in a console log or alert message using Javascript?

4
  • Regex would be a good choice. Commented Mar 31, 2022 at 21:27
  • what is Regex im sorry? Commented Mar 31, 2022 at 21:30
  • Are you sure there is an extra trailing }? Commented Mar 31, 2022 at 21:31
  • See Regular Expressions or similar Commented Apr 1, 2022 at 13:43

2 Answers 2

1

You could use String#match to extract the wanted part.

let str = '{\n "modelColor": ["000019000031262"]\n}"}';
let res = str.match(/\["(.*)"]/)[1]; // get first captured group
console.log(res);

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

Comments

1

You could try:

const str = `{\\n "modelColor": ["000019000031262"]\\n}"}`
const regex = /\[(.*)\]/gm;

console.log(regex.exec(str)[1])

But it seems most likely that the api is actually returning JSON (and that maybe you have clipped it). If so. Use JSON.parse to convert the string to an object

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.