callback function in javascript replace() is not called

Obay

I would like to replace the domain of a cookie string:

var cookieText = 'test=value; path=/; domain=.mydomain.com';

cookieText.replace(/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);/, function(match, dot, domain){
    return dot + myfunction(domain)
});

But it seems mycallback function is never called. What is wrong with the code?

Danilo Valente

That's because you regex doesn't match any substring in your cookieText string. This happens because you pattern expects a ; at the end of each substring, but that doesn't happen to domain=.mydomain.com. Try this regex instead:

/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);?/

Then it will work:

var cookieText = 'test=value; path=/; domain=.mydomain.com';

cookieText.replace(/[Dd]omain=(\.?)([a-zA-Z0-9\.]*);/, function(match, dot, domain){
    console.log(match);
    return dot + myfunction(domain);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

callback function is not called in JSONP

From Dev

Success callback function is not called

From Dev

Javascript .click() callback function apparently not called in Bootstrap accordion?

From Dev

JavaScript "this" context in .replace callback

From Dev

Callback function being called twice

From Dev

Woocommerce callback function is not getting called

From Dev

Javascript - Regex. Replace all spaces in a string, but called from a function

From Dev

JavaScript Multiple Callback Function

From Dev

Javascript array with callback function

From Dev

Usage of Javascript callback function

From Dev

Javascript Function with Callback and Parameters

From Dev

javascript callback on function

From Dev

JavaScript: custom callBack function

From Dev

Javascript with callback function not working

From Dev

OOP with Javascript and callback function

From Dev

Javascript callback function not work

From Dev

Creating a callback on javascript function

From Dev

Javascript anonymous callback function

From Dev

javascript callback function selection

From Dev

Javascript Callback function malfunction

From Dev

Callback with arrow function in javascript

From Dev

Javascript Custom Callback Function

From Dev

Callback before RequireJS "define" function is called?

From Dev

SetWindowsHookEx succeed but callback function is never called

From Dev

Callback function not get called in jasmine test code

From Dev

Callback function called twice and more on Bootstrap Modal

From Dev

Cakephp Callback function called multiple times

From Dev

Callback function of tokenizeCard it's not called on Braintree

From Dev

Callback function called twice and more on Bootstrap Modal

Related Related

HotTag

Archive