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)
Related
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 2 years ago.
Improve this question
I'll make this extremely simple
I have a function that comes across a string as such by design: water 'thing one'
I need to split this string by the first space, since there are spaces elsewhere.
I've tried many regex expressions like / .*? /, but they can only match two consecutive spaces.
How do I do this?
Thanks in advance.
If you just want the portion of the string before and after the first space, you could use regex replacement here:
var input = "water 'thing one'";
var first = input.replace(/[ ].*$/, "");
var second = input.replace(/^\S*[ ]/, "");
console.log("first part: " + first);
console.log("second part: " + second);
You can capture them using String#match with this regex:
/(.*?) (.*)/
const text = "water 'thing one'";
const [, first, second] = text.match(/(.*?) (.*)/);
console.log('first:', first);
console.log('second:', second);
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 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 want the text which is outside of brackets, for eg.
Text is - Outside (inside)
and what I expect is - Outside
Can someone please help me to achieve this.
You can use slice & use indexOf to get the first (. This will extract all the characters before first (
let str = 'Outside (inside)'
let substr = str.slice(0, str.indexOf('('));
console.log(substr.trim())
If you wanted to remove all bracketed text from the string you could use
let str = 'Outside (inside)test(d 342 dd3d)dd(t423t t)dd()fasf(fsdfds32dfs)';
console.log(str.replace(/(\([\w\d ]*\))+/g, ''))
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 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.