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.
Related
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)
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);
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 5 years ago.
Improve this question
Could someone with more regex experience than me help me out?
return path.replace(/\//g, '.').replace(/^\./, '');
I have found this regex in a js file within a giant app. The JS when run through npm node-minify or any of the others sees it as a comment and turns it into this:
return path.replace(/\g, '.').replace(/^\./, '');
I get the first bit is replacing all \ with a . and the second bit trims any leading . from the string. Can i change this so the regex pattern is wrapped in quotes?
Just use the RegExp constructor and quote your pattern.
const path = '/usr/bin/env';
const matchSlash = new RegExp('/', 'g');
const translate = path => path.replace(matchSlash, '.').replace(/^\./, '');
console.log(translate(path));
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 6 years ago.
Improve this question
I have this string:
"This thing (123, 12) (2005.03 - 2011.12)"
I want to convert it to:
"This thing (2005.03 - 2011.12)"
Meaning remove text between two first parenthesis: (123, 12). But only if there are two or more parenthesis following in same string. So a string like
"Another thing (2005.05 - 2011.08)"
should be left as it is.
How can I do it with javascript?
You can use String.replace() with regex like this https://regex101.com/r/X7ioxu/1
var regex = /(\(.+?\))\s?\(/g;
var str1 = "This thing (123, 12) (2005.03 - 2011.12)";
var str1 = "This thing (2005.03 - 2011.12)";
alert(str1.replace(regex,'('));
alert(str2.replace(regex,'('));
With the data given, this works
var str = "This thing (123, 12) (2005.03 - 2011.12)";
var parts = str.split(/(?=\()/g); // split on ( with lookahead
if (parts.length==3) parts.splice(1,1)
str = parts.join("")
console.log(str)
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);