0

I'm using the NewsApi API as content for a Typing Tutor app, which requires replacing characters that are not easily inputted (specifically, , ,,, which need to be replaced with ',-,",", respectively).

I suspect the strings coming through are somehow encoded because the javascript string.replace() function is not working on strings pulled directly from the HTTP responses - it only works if I copy-paste the relevant strings from the API responses in the browser network tools or from Postman.

Code

(in practice the string is pulled from the NewsApi API but it is a difficult issue to replicate as the news headlines are always changing...)

var string = "you’re"
string =  string.replace(/’/,`'`)
console.log(string)

Expected Output

This only happens when the string is copy-pasted from browser console or postman:

>>  you're

Actual Output

>>  you’re
// note the apostrophe has a direction (not vertical) and
// therefore can't be easily typed

EDIT: there was a spurious space in the regex before the apostrophe. This was a typo in the question - not in the original codebase - and has now been fixed.

3
  • Remove the whitespace in the regular expression. Commented Apr 7, 2020 at 10:28
  • EDIT: there was a spurious space in the regex before the apostrophe. This was a typo in the question - not in the original codebase - and has now been fixed. The problem still remains :( Commented Apr 8, 2020 at 4:31
  • "you’re".replace(/’/,') works well. Without more details or a minimal reproducible example, there is nothing we can help you with. Commented Apr 8, 2020 at 6:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.