Is there a way to remove all characters within two slashes? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to use only regex to remove '/please-remove-this/' and replace '%20' with ' '.
let str = '/please-remove-this/Hello%20world'
let strNew = str.replace(/%20/g, ' ').substring(20)
strNew = 'Hello world'
'Hello world' is the correct output but I feel there is a more efficient way to do this with regex only

Rather replacing %20 you can decode using decodeURI
let str = '/please-remove-this/Hello%20world';
let out = decodeURI(str.replace(/\/.*\//g, ''));
console.log(out)
Using only regex
let str = '/please-remove-this/Hello%20world';
let out = decodeURI(str.replace(/\/.*\/(.*)%20(.*)/, '$1 $2'));
console.log(out)

Related

Javascript remove all ',' in a string with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i want remove all ',' from my string with regex in javascript.
this is an example from my string:
45,454,545
and i want my string convert to this:
45454545
Comma isn't a special character in regex, so you can just use /,/. Add the global flag and you're done.
console.log('45,454,545'.replace(/,/g, ''))
Try this,
var str = "45,454,545";
var res = str.replace(/,/g, "");
console.log(res);

How to show first 2 characters and replace all the last by * [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);

How could I split a string in the format 'htmlElement#idName.className.anotherClassName'? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a string like that header#top.header.header--show-offset and I'm struggling to know how could I split it into something like [ 'header', '#top', '.header', '.header--show-offset' ]
Thank you!
You can use regex:
let str ="header#top.header.header--show-offset";
// Keep the delimiter
let res = str.split(/(?=[#.])/gi);
console.log(res);
I would do this in two steps:
Replace id and class symbols with a comma and then the symbol
Split the resulting string by comma
var selectorString = "header#top.header.header--show-offset";
selectorString = selectorString.replace(/#/g, ",#")
selectorString = selectorString.replace(/\./g, ",.");
var selectorList = selectorString.split(",");
console.log(selectorList);

split a string, breaking at a different character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the String:
Name01: Name02 - Project Name (Client) - Infos
Using JavaScript, what is the fastest way to parse this into:
Name01
Name02
Project Name
Client
Infos
You can replace your string with a common character where ever you need. So that you can split on them. Try the following way:
var str = "Name01: Name02 - Project Name (Client) - Infos"
str = str.replace(/[-()]/g,':').split(':');
str = str.filter(i => i.trim()).map(j => j.trim());
console.log(str);
This isn't perfect but its simple:
const str = 'Name01: Name02 - Project Name (Client) - Infos';
const matches = str
.replace(/[^\w\s+]/gi, '')
.replace(/\s\s+/gi, ' ')
.split(' ');
console.log(matches);
The issue here is keeping the space between Project Name.

Split string using javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to split${SOMENAME} (${THISNAME}) ${THESENAME}
I just need to extract the words SOMENAME THISNAME and THESENAME from the above string. Is it possible?
You can pass in a regular expression separator as part of the .split() function.
var string = "${SOMENAME} (${THISNAME}) ${THESENAME}";
var re = /\W+/;
var arr = string.split(re);
document.write(arr);
Take a look at String.prototype.split for more information.
If you only need extract the words, this could be a simple solution:
var s = "${SOMENAME} (${THISNAME}) ${THESENAME}";
var words = s.match(/([A-Z])\w+/g);

Categories