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);
Related
I'm new to regex and trying to figure out how to remove characters till the last - in the string. I currently have strings in the format like this:
purple-hoodie.jpg-1625739747918
I am trying to remove characters to essentially be left with:
-1625739747918
Does anyone have any advice on how to approach this? I'm struggling to work out how to indicate to reach the last - in the string, if that is even possible?
Thanks
Just use lastIndexOf
let str = 'purple-hoodie.jpg-1625739747918'
console.log(str.substring(str.lastIndexOf('-')))
I prefer a match approach here:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.match(/-\d+$/)[0];
console.log("match is: " + output);
But this assumes that the input would end in all digits. A more general regex approach might use a replace all:
var input = "purple-hoodie.jpg-1625739747918";
var output = input.replace(/^.*(?=-)/, "");
console.log("match is: " + output);
Here is my solution.
const txt = 'purple-hoodie.jpg-1625739747918';
const result = txt.replace(/-\d+$/, '');
console.log(result)
This removes the last trailing digits prefixed by -.
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 ?
I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');
I have the following strings
"www.mywebsite.com/alex/bob/a-111/..."
"www.mywebsite.com/alex/bob/a-222/..."
"www.mywebsite.com/alex/bob/a-333/...".
I need to find the a-xxx in each one of them and use it as a different string.
Is there a way to do this?
I tried by using indexOf() but it only works with one character. Any other ideas?
You can use RegExp
var string = "www.mywebsite.com/alex/bob/a-111/...";
var result = string.match(/(a-\d+)/);
console.log(result[0]);
or match all values
var strings = "www.mywebsite.com/alex/bob/a-111/..." +
"www.mywebsite.com/alex/bob/a-222/..." +
"www.mywebsite.com/alex/bob/a-333/...";
var result = strings.match(/a-\d+/g)
console.log(result.join(', '));
Use the following RegEx in conjunction with JS's search() API
/(a)\-\w+/g
Reference for search(): http://www.w3schools.com/js/js_regexp.asp
var reg=/a-\d{3}/;
text.match(reg);
Here's an example string:
survey_questions_attributes_1317784471568_answers_attributes
I want to get the 1317784471568 out of the string.
But that part could also be text. For example, I may also need to get a string called new_questions from that same spot in the string.
The constant here is that survey_questions_attributes_ will always precede the chunk I want and _answers_attributes will always follow it.
id = str.match(/_questions_attributes_(.+)_answers_attributes/)[1];
var str = "survey_questions_attributes_1317784471568_answers_attributes";
var newStr = str.replace("survey_questions_attributes_", "");
newStr = newStr.replace("_answers_attributes", "");
Easiest way I can think of without using regular expressions or the like.
Try -
var text = "survey_questions_attributes_1317784471568_answers_attributes"
.replace("survey_questions_attributes_","")
.replace("_answers_attributes","")
Use substring():
mystring = "survey_questions_attributes_1317784471568_answers_attributes"
newstring = mystring.substring(28, 41)