I have the following code:

var requestUri = webAbsUrl + "/_api/web/Lists/getByTitle('Staff Directory')/Items?$\
        filter=Title eq '" + currentUserEmail + "'&$\

It works until it encountered an employee with an apostrophe in the last name. I tried encodeURI() and encodeURIComponent() but neither worked. I tried searching online and can't seem to find a quick solution.

Example Email: John.O'Doe@domain.com


Use below:

var str = fixedEncodeURIComponent(currentUserEmail);

var requestUri = webAbsUrl + "/_api/web/Lists/getByTitle('Staff Directory')/Items?$\
    filter=Title eq '" + str + "'&$\

function fixedEncodeURIComponent(src) {
return encodeURIComponent(src).replace(/[']/g, function (c) {
        return '%' + c.charCodeAt(0).toString(16) + '%' + c.charCodeAt(0).toString(16);
  • does it mean I have to replace a single apostrophe with double apostrophe? I'm confused about how charCodeAt(0).toString(16) = 27? – Tamras Merin May 2 '18 at 19:15
  • I found the answer about .toString(16) here: – Tamras Merin May 2 '18 at 19:25

Deepmala's answer will work, but it seems overly complex and hides both the approach and the actual solution. What you're trying to do is replace a single apostrophe with two, but they both have to be URI-encoded.

A simpler and easier-to-understand solution:

function fixedEncodeURIComponent(src) {
    return encodeURIComponent(src).replace(/[']/g, '%27%27');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.