Regex/Replace any chars inside match combination - javascript

For example
var string = 'width="300" height="650"'
i would like to update height in that string and get something like
string = string.replace(/height="..."/g, 'height="150"');
//... as any symbol
how to make reg expression who does not care value of height, to replace it with new one?
You can play with it here, in example:
JsFiddle

var string = 'width="300" height="650"';
string = string.replace(new RegExp(/height=\"[0-9]+\"/g), 'height="150"');
DEMO

for case insensitive use:
.replace(new RegExp(/height=\"[0-9]+\"/gi), 'height="150"');

Related

Need to replace a string from a long string in javascript

I have a long string
Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
removable_str2 = 'ab#xyz.com;';
I need to have a replaced string which will have
resultant Final string should look like,
cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
I tried with
str3 = Full_str1.replace(new RegExp('(^|\\b)' +removable_str2, 'g'),"");
but it resulted in
cab#xyz.com;c-c.c_ab#xyz.com;
Here a soluce using two separated regex for each case :
the str to remove is at the start of the string
the str to remove is inside or at the end of the string
PS :
I couldn't perform it in one regex, because it would remove an extra ; in case of matching the string to remove inside of the global string.
const originalStr = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;ab#xyz.com;c_ab#xyz.com;';
const toRemove = 'ab#xyz.com;';
const epuredStr = originalStr
.replace(new RegExp(`^${toRemove}`, 'g'), '')
.replace(new RegExp(`;${toRemove}`, 'g'), ';');
console.log(epuredStr);
First, the dynamic part must be escaped, else, . will match any char but a line break char, and will match ab#xyz§com;, too.
Next, you need to match this only at the start of the string or after ;. So, you may use
var Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
var removable_str2 = 'ab#xyz.com;';
var rx = new RegExp("(^|;)" + removable_str2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "g");
console.log(Full_str1.replace(rx, "$1"));
// => cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
Replace "g" with "gi" for case insensitive matching.
See the regex demo. Note that (^|;) matches and captures into Group 1 start of string location (empty string) or ; and $1 in the replacement pattern restores this char in the result.
NOTE: If the pattern is known beforehand and you only want to handle ab#xyz.com; pattern, use a regex literal without escaping, Full_str1.replace(/(^|;)ab#xyz\.com;/g, "$1").
i don't find any particular description why you haven't tried like this it will give you desired result cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
const full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
const removable_str2 = 'ab#xyz.com;';
const result= full_str1.replace(removable_str2 , "");
console.log(result);

Using RegExp to substring a string at the position of a special character

Suppose I have a sting like this: ABC5DEF/G or it might be ABC5DEF-15 or even just ABC5DEF, it could be shorter AB7F, or AB7FG/H.
I need to create a javascript variable that contains the substring only up to the '/' or the '-'. I would really like to use an array of values to break at. I thought maybe to try something like this.
...
var srcMark = array( '/', '-' );
var whereAt = new RegExp(srcMark.join('|')).test.str;
alert("whereAt= "+whereAt);
...
But this returns an error: ReferenceError: Can't find variable: array
I suspect I'm defining my array incorrectly but trying a number of other things I've been no more successful.
What am I doing wrong?
Arrays aren't defined like that in JavaScript, the easiest way to define it would be with:
var srcMark = ['/','-'];
Additionally, test is a function so it must be called as such:
whereAt = new RegExp(srcMark.join('|')).test(str);
Note that test won't actually tell you where, as your variable suggests, it will return true or false. If you want to find where the character is, use String.prototype.search:
str.search(new RegExp(srcMark.join('|'));
Hope that helps.
You need to use the split method:
var srcMark = Array.join(['-','/'],'|'); // "-|/" or
var regEx = new RegExp(srcMark,'g'); // /-|\//g
var substring = "222-22".split(regEx)[0] // "222"
"ABC5DEF/G".split(regEx)[0] // "ABC5DEF"
From whatever i could understand from your question, using this RegExp /[/-]/ in split() function will work.
EDIT:
For splitting the string at all special characters you can use new RegExp(/[^a-zA-Z0-9]/) in split() function.
var arr = "ABC5DEF/G";
var ans = arr.split(/[/-]/);
console.log(ans[0]);
arr = "ABC5DEF-15";
ans = arr.split(/[/-]/);
console.log(ans[0]);
// For all special characters
arr = "AB7FG/H";
ans = arr.split(new RegExp(/[^a-zA-Z0-9]/));
console.log(ans[0]);
You can use regex with String.split.
It will look something like that:
var result = ['ABC5DEF/G',
'ABC5DEF-15',
'ABC5DEF',
'AB7F',
'AB7FG/H'
].map((item) => item.split(/\W+/));
console.log(result);
That will create an Array with all the parts of the string, so each item[0] will contain the text till the / or - or nothing.
If you want the position of the special character (non-alpha-numeric) you can use a Regular Expression that matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_], that is: \W
var pattern = /\W/;
var text = 'ABC5DEF/G';
var match = pattern.exec(text);
var position = match.index;
console.log('character: ', match[0]);
console.log('position: ', position);

Replace in javascript

I have a string something like this:
http://stackoverflow.com/questions/ask
And would like to return this part:
http://stackoverflow.com/questions/
How can I do this using pure javascript?
Thanks!
This will match and remove the last part of a string after the slash.
url = "http://stackoverflow.com/questions/ask"
base = url.replace(/[^/]*$/, "")
document.write(base)
Help from: http://www.regexr.com/
For slicing off last part:
var test = 'http://stackoverflow.com/questions/ask';
var last = test.lastIndexOf('/');
var result = test.substr(0, last+1);
document.write(result);
You can accomplish this with the .replace() method on String objects.
For example:
//Regex way
var x = "http://stackoverflow.com/questions/ask";
x = x.replace(/ask/, "");
//String way
x = x.replace('ask', "");
//x is now equal to the string "http://stackoverflow.com/questions/"
The replace method takes two parameters. The first is what to replace, which can either be a string or regex, literal or variable, and the second parameter is what to replace it with.

Removing spaces, hyphen and brackets from a string

I am getting a string like:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
I want to remove the spaces, hyphen and brackets from every number. Something like:
var str = '+911234567891,432123234,12312313456,4325671234';
Please suggest a way to achieve this.
This will do your job:
var str = '+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)';
var result = str.replace(/[- )(]/g,'');
alert(result);
You can use Regular Expression to replace those items by empty string:
'+91 1234567891,(432)123234,123-123-13456,(432)(567)(1234)'.replace(/[\s()-]+/gi, '');
// results in "+911234567891,432123234,12312313456,4325671234"
Hope it helps.

How to remove empty spaces in the url

Hi how can i remove empty space in the URL using javascript:
here how it looks like
stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000
This is what i want:
stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000&sealA=255146-000&terminalB=916876-010&sealB=255146-000
Just decode the url, use the replace function to eliminate the whitespaces and then encode it again.
function removeSpaces(url) {
return encodeURIComponent(decodeURIComponent(url).replace(/\s+/g, ''));
}
JS replace-Function to eliminate whitespaces: How to remove spaces from a string using JavaScript?
Javascript decode and encode URI: http://www.w3schools.com/jsref/jsref_decodeuri.asp
If theString has your original string, try:
theString = theString.replace(/%20/g, "")
you can simply replace the substring you want ("%20") with nothing (or "").
Try something like this:
var str = "stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000"
var res = str.replace(/%20/g, "");
Hope it helps.
The idea is just the 1%, now go make the 99% !
try this function :
function Replace(str) {
var del = new RegExp('%20');
str.match(del);
}
This Might Help you:
function doIt(){
var url="stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000";
var str=url.toString();
str=str.split("%20").join("");
return str;
}
Here is Working Fiddle.
You will need to treat the URL as string and remove the whitespace inside that string using your programming language.
For example, if you use JavaScript to remove the whitespace from the URL you can do this:
let string = `https://example.com/ ?key=value`;
string.replace(/\s+/g, "");
This will give you: https://example.com/?key=value;

Categories