RegEx with any number replace by empty string - javascript

I am totally not confirm with Regular Expressions, so may you guys can help me out.
I have a String like "blablabla_300x300.jpg" where 300 can be any number
I need to replace the "_300x300" with ""
Can please someone provide me the correct answer (Javascript)
Thank you so much

Try this:
var s = "blablabla_300x300.jpg";
s = s.replace(/(_\d+x\d+)(\.jpg)$/, "$2");
console.log(s);

Related

Change group of characters in a String

Let's say I have this String : str = '236112456'
I want to change '236' with something else, and if a character is alone, change by another thing.
i tried something like that :
var result =
str.replaceAll('236', '236.jpg')
.replaceAll('1', '1.jpg')
.replaceAll('2', '2.jpg')
.replaceAll...
but it won't take 236 and only change individual letters...
How can I achieve that ?
Thanks in advance for your help :)
[edit] - Mistakes in the code example
Here is the solution.
Please use regex expression instead of '23' string.
str = '236112456';
var result = str.replaceAll(/236/gi, '236.jpg').replaceAll(/1/gi, '1.jpg');
...
The result is "236.jpg1.jpg1.jpg2456". Is this not what you are looking for ?

How to get base64 code from css rule

I've got a string 'url(data:image/png;base64,iVBORw0K...GgoA)'.
I need to invoke only base64 data from it. In output i'd like to see something like this ['iVBORw0K...GgoA'].
Could anyone help me with creating a correct RegExp expression?
Thanks in advance.
.*base64,(\w+)\)$
If you get group 1 from the regex, you will get the base64 data you want.
You can write a regular expression like this,
var phrase = "url(data:image/png;base64,iVBORw0K...GgoA)";
var myRegexp = /base64,(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);
HTH

Add _ when there is a space

I am trying to split a string up. I need to somehow take out white space and replace it with _
So for instance:
Jiffy Lube
but I want it to return
Jiffy_lube
Does this require regex? or do I do something like .split('').join('');
Im not really sure any help would be very appreciated! Thank you!
Example:
Dicks Sporting Goods
return:
Dicks_Sporting_Goods
THANK YOU ALL FOR YOUR HELP! IM SORRY THIS IS A POOR QUESTION. I UNDERSTAND NOW WHY ITS A POOR QUESTION. I WILL STILL MARK ANSWERED THOUGH.
Yes, that may sound strange but the easiest way to replace a single character more than once in a string is to use a regular expression.
Use replace :
str = str.replace(/\s/g,'_')
You could also use split and join :
str = str.split(' ').join('_')
but that would be both less direct and slower.
As you said str.split(' ').join('_')
maybe this will help:
var mystr = "Dicks Sporting Goods"
alert(mystr.replace(/\s/g,"_"))
// Dicks_Sporting_Goods

Help with a regular expression to capture numbers

I need to capture the price out of the following string:
Price: 30.
I need the 30 here, so I figured I'd use the following regex:
([0-9]+)$
This works in Rubular, but it returns null when I try it in my javascript.
console.log(values[1]);
// Price: 100
var price = values[1].match('/([0-9]+)$/g');
// null
Any ideas? Thanks in advance
Try this:
var price = values[1].match(/([0-9]+)$/g);
JavaScript supports RegExp literals, you don't need quotes and delimiters.
.match(/\d+$/) should behave the same, by the way.
See also: MDN - Creating a Regular Expression
Keep in mind there are simpler ways of getting this data. For example:
var tokens = values[1].split(': ');
var price = tokens[1];
You can also split by a single space, and probably want to add some validation.
Why don't you use this?
var matches = a.match(/\d+/);
then you can consume the first element (or last)
my suggestion is to avoid using $ in the end because there might be a space in the end.
This also works:
var price = values[1].match('([0-9]+)$');
It appears that you escaped the open-perens and therefore the regex is looking for "(90".
You don't need to put quotes around the regular expression in JavaScript.

Help For A Simple Regular Expression

i want to extract 34 from this string. How Can i done that ? (i will use javascript)
#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30
var src = "#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30",
match = /project_subcategory=(\d+)/g.exec(src);
alert(match[1]);
Anyways, it looks like a query string so there should be a better way to parse/read that string. See http://blog.falafel.com/Blogs/AdamAnderson/07-12-17/Parse_a_Query_String_in_JavaScript.aspx
.*project_subcategory=(\d*).*

Categories