I have a string like this:
const myString = "["dupa", "dupa", "dupa"]";
how to use regex in JS/TS to remove all characters " and [ and ]?
i need only dupa, dupa, dupa
thanks for any help
It will return you string with other removed from it and with your double quoted there is problem with it. So use single quote.
const myString = '["dupa", "dupa", "dupa"]';
const a = myString.replace(/[\[\]']+/g, '').replace(/"/g, '');
console.log(a);
You will get "dupa, dupa, dupa"
Or you can go with :
const myString = '["dupa", "dupa", "dupa"]';
const a = JSON.parse(myString);
console.log(a.join(", "))
Related
I had following string ,
const str= "this is harp//foo.eps and harp//data.eps"
I need to replace the string between harp to .eps
My code below,
const str = "this is harp//foo.eps and harp//data.eps"
const data = str.replace(/\harp.*\eps/, 'www.imge.jpg');
console.log(data);
it was returning output below,
this is www.imge.jpg
But expected output
this is www.imge.jpg and www.imge.jpg
Try to make use non-greedy (?) search and global (/g) modifier:
const str = "this is a harp//foo.eps and a harp//data.eps and harp//data.eps"
const data = str.replace(/harp(.*?)eps/g, 'www.imge.jpg');
console.log(data);
You can do it using the replace all function and the following regex
const str = "this is harp//foo-two.eps and harp//data-9.eps"
const newSTr = str.replaceAll(/harp\/\/[A-Za-z\-0-9]+\.eps/g, 'www.imge.jpg')
console.log(newSTr);
Given the following string:
const myString = "This is my comment content. [~firstName.lastName]";
What is a javascript regex to extract the "firstName" and "lastName"?
What is a javascript regex to extract the content minus the "[~firstName.lastName]"?
I'm rubbish with Regex.
const myString = "This is my comment content. [~firstName.lastName]";
const first = myString.match(/.*\~(.*)\./)[1]
const last = myString.match(/.*\.(.*)]/)[1]
const both = myString.match(/.*\~(.*)\]/)[1]
console.log(first)
console.log(last)
console.log(both)
Here's a good website for regex help
You can try this-
let myString = "This is my comment content. [~firstName.lastName] and the original name is [~John.Doe]";
const regex = /\[~([^\]]+)\.([^\]]+)\]/g;
let match = regex.exec(myString);
const names = [];
while(match !== null) {
names.push({firstName: match[1], lastName: match[2]});
match = regex.exec(myString);
}
// Remove the pattern from the original string.
myString = myString.replace(regex, '');
console.log(names, myString);
This code will find all the matches found for the pattern.
You could target each name separately. match will return the complete match as the first element of the array, and the groupings specified by the ( and ) in the regex as the subsequent elements.
const str = 'This is my comment content. [~firstName.lastName]';
const regex = /\[~(.+)\.(.+)\]/;
console.log(str.match(regex));
Or you could target just the characters within the brackets and then split on the ..
const str = 'This is my comment content. [~firstName.lastName]';
const regex = /\[~(.+)\]/;
console.log(str.match(regex)[1].split('.'));
I know to get a substring between 2 characters we can do something like myString.substring(myString.lastIndexOf("targetChar") + 1, myString.lastIndexOf("targetChar"));
But how would I get a substring if the two targetChar are the same. For example, if I have something like const myString = "/theResultSubstring/somethingElse", how would I extract theResultSubstring in this case?
Do indexOf and lastIndexOf if there are only two characters in your string:
const myString = "/theResultSubstring/somethingElse";
const subString = myString.substring(myString.indexOf("/") + 1, myString.lastIndexOf("/"));
console.log(subString);
Or split it:
const myString = "/theResultSubstring/somethingElse";
const subString = myString.split("/")[1];
console.log(subString);
You could use String#match instead with following regex and positive & negative lookaheads.
const myString = "/theResultSubstring/somethingElse";
const res = myString.match(/(?!\/)\w+(?=\/)/)[0];
console.log(res);
I have the following string:
let str = '/user/:username/'
I want to extract replace username with harry with colon removed.
I tried the following:
const regex = /[^:]+(?=:)/g
str.replace(regex, x => console.log(x))
Try: /:\w+/
let str = '/user/:username/'
str.replace(/:\w+/, "harry")
// => "/user/harry/"
let str = '/user/:username/';
let html = str.replace(":username", "harry");
console.log(html);
var str = '/user/:username/';
var newstr = str.replace(/:username/i, "harry");
print(newstr);
hi pal, is this what you are looking for? i found it at https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/replace
you can use like that :
let str = '/user/:username/';
str.replace(':username','harry')
// o/p => /user/harry/"
In your regex [^:]+(?=:) you are matching 1+ times not a colon and assert that at the end there should be a colon resulting in a match for /user/
If you want to use the negated character class you could match a colon and then not a forward slash:
:[^\/]+
const str = `/user/:username/`;
const result = str.replace(/:[^\/]+/, "harry");
console.log(result);
If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");