2

i have this string : 4.5 von 5 Sternen.

My desired output : 4.5/5

What I tried entry.rating.replace(/[a-z A-Z]/g , '/'); Which resulted in 4.5/5/ I dont need that extra slash just the one in the middle dividing the two numbers. I would love some help right now . Thanks in advance.

1
  • So that means you use /[a-z A-Z]+/g Commented Nov 6, 2019 at 15:19

1 Answer 1

2

You may use match + join here:

var s = '4.5 von 5 Sternen.';
var arr = s.match(/\b\d+(?:\.\d+)?/g);
var out = null;

if (arr != null)
  out = arr.join('/');

console.log(out);
//=> 4.5/5

  • We are using regex \b\d+(?:\.\d+)? in .match to match a number that may be integer or floating point number.
  • Once we have array generated from match we use join('/') to get desired output.
Sign up to request clarification or add additional context in comments.

1 Comment

You might want to implement some error checking in case of no match. String#match will return null if no match is found.

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.