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 ?
Related
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);
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
I will have one string mixed with dots and lowercase letters like:
string = '..a..d..h.w';
I want it to have all letters moved to the most right possible:
result = '.......adhw';
I would really appreciate a short solution for this ;)
I was trying to use regex:
result = string.replace(/(\.)|(.)/g,'$1$2');
But without luck, it remains the same.
Any help is welcome.
You could strip all of the dots and then strip all of the non-dots and concatenate them :
var output = input.replace(/[^\.]/g,'') + input.replace(/\./g,'');
A simple string manipulation for this could be string.count('.')*'.'+string.replace('.','')
you can try this:
var str = '..a..d..h.w';
var result = ("..........." + str.replace(/\./g, '')).slice(-str.length);
I have some javascript that I need assistance with where I want to update a string with javascript.
Original string:
987654321-200x200-1_This+is+text.jpg
Want it to end up to be:
not_found-200x200.jpg
So 987654321 gets replaced with not_found and -1_This+is+text with nothing.
Note the original string is fully dynamic with only the - x - _ + constant in all.
I have tried something like this:
'987654321-200x200-1_This+is+text.jpg'.replace(/\_\d{0,}[A-Za-z]*/, '_not_found')
but need help with the regexp to achieve this. Anyone help?
Not sure if this will do, but if all you're looking for is 200x200 you could just split on - and use that :
var str = '987654321-200x200-1_This+is+text.jpg';
var not = 'not_found-' + str.split('-')[1] + '.jpg';
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*).*