This question already has answers here:
Fastest method to replace all instances of a character in a string [duplicate]
(14 answers)
Closed 6 years ago.
New to jQuery & JavaScript.
I have
var x = location.pathname;
(ex: /abc/collection/tea/green/index.php)
Like this I have various pathname retrieved using location.pathname.
I want to replace all "/" in the pathname with ":" (I mean a / with a :) and also I don't want the .php which is at the end. Any help please.
This has nothing to do with jQuery. You can use JavaScript to replace your string.
Like this:
var x = location.pathname;
x = x.replace(/\//g, ":");
or just
var x = location.pathname.replace(/\//g, ":");
You can also use the same method to remove the ".php" by adding this:
x = x.replace(/\.php$/i, "");
(assuming you only need to replace it once at the end)
Basically you have to use the regex version of str.replace() and the g (global) switch to do a replace all. Use the str.replace(/texthere/g, "replacement") pattern to replace more than one occurrence - just remember to properly escape 'texthere' so characters don't conflict.
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 1 year ago.
Write a function normalize, that replaces '-' with '/' in a date string.
Example: normalize('20-05-2017') should return '20/05/2017'.
This is what I wrote:
function normalize(str) {
let replaced = str.replace('-', '/')
return replaced
}
I can't replace the other - with / can someone explain how to do this?
When you use replace, only the first instance is replaced. See replace
What you can do is either
Use replaceAll
const replaced = str.replaceAll('-', '/');
Or
const replaced = str.replace(/-/g, '/');
The g means Global, and causes the replace call to replace all matches, not just the first one
Instead of str.replace('-','/') use str.replaceAll('-','/')
This question already has answers here:
Replace all occurances in javascript
(4 answers)
Closed 2 years ago.
I have a string that contains multiple '-'. I want to remove the all '-'. I tried the following way but it didn't work.
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984"
var new_str = str.replace('-', '')
How can I remove or replace all the '-'? Is there any simple function for that?
Remove globally and remove the second parenthesis
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984";
var new_str = str.replace(/-/g, '');
console.log(new_str);
This question already has answers here:
Preg_match to regex equivalent expression to match any Unicode letters
(2 answers)
Match only unicode letters
(3 answers)
Closed 4 years ago.
I nedd to add a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ x time but I find this very ugly. So I try \p{L} but it does not working in JavaScript.
Any Idea ?
my actual regex : [a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ][a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ' ,"-]*[a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ'",]+
I want to have a thing like that : [\p{L}][\p{L}' ,"-]*[\p{L}'",]+ (or smaller than the actual expression)
What you need to add is a subset of what you asked for. First you should define what set of characters you need. \pL means every letter from every language.
It's kind of ugly but doesn't affect performance and rather the best solution to get around such kind of problems in JS. ECMA2018 has a support for \pL but way far to be implemented by all major browsers.
If it's a personal taste, you could reduce this ugliness a bit:
var characterSet = 'a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ';
var re = new RegExp('[' + characterSet + ']' + '[' + characterSet + '\' ,"-]*' + '[' + characterSet + '\'",]+');
This update credits go to #Francesco:
var pCL = 'a-zA-ZáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ';
var re = new RegExp(`[${pCL}][${pCL}' ,"-]*[${pCL}'",]+`);
console.log(re.source);
You have XRegExp addon to support unicode letter matcher:
var unicodeWord = XRegExp("^\\pL+$"); // L: Letter
Here you can see more example matching unicode in javascript
http://xregexp.com/plugins/
This question already has answers here:
Why isn't this split in javascript working?
(2 answers)
Closed 8 years ago.
please, could you help me with my task: I need to replace part of string and probably the best way is regular expression but I don't know, how to make it working. I want to do this:
http://someweb.com/section/&limit=10&page=2
replace page=2 with page=3 so string will be:
http://someweb.com/section/&limit=10&page=3
I tried to do something like this:
// set string in t variable
t.replace('/page=[0-9]/', 'page=$1++') });
Thank you very much for your help :)
In our case first argument should be regexp, but in your variant this is string '/page=[0-9]/' (remove '). In replace you can pass function as second argument, and do with matched data what you want. (for example add +1 to page=)
var str = "http://someweb.com/section/&limit=10&page=2";
str.replace(/page=(\d+)/, function (match, page) {
return 'page=' + (+page + 1); // plus before page converts string to number
});
Example
You can also try below code.
var url = "http://someweb.com/section/&limit=10&page=2",
reExp = /page=([0-9])+/,
result = reExp.exec(url);
url = url.replace(reExp, 'page=' + (+result[1] + 1));
console.log(url)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript replace all / with \ in a string?
I have a date='13/12/2010' I want to replace this '/' to '_' or something else.
I read this But I do not know how can that applied for my case .
Use a global RegEx (g = global = replace all occurrences) for replace.
date = date.replace(/\//g, '_');
\/ is the escaped form of /. This is required, because otherwise the // will be interpreted as a comment. Have a look at the syntax highlighting:
date = date.replace(///g, '_');
One easiest thing :)
var date='13/12/2010';
alert(date.split("/").join("_")); // alerts 13_12_2010
This method doesn't invoke regular expression engine and most efficient one
You can try escaping the / character like this -
date.replace( /\//g,"_");