Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
var input="gazanie, flower, blossom";
I need to replace input using JavaScript or jQuery.
Output should be like:
"#gazanie #flower #blossom"
"gazanie, flower, blossom".split(',').map((e)=>`#${e.trim()}`).join('');
THat's it :
split : to split string according to delimitor which is , & get array
map : to add prefix for each element which is #
trim : eliminate space if it is the 1st char or/and is the last char.
join : to return back from Array to String .
DEMO :
var input="gazanie, flower, blossom";
var output=input.split(',').map((e)=>`#${e.trim()}`).join(' ');
console.log(output);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I have a string that looks like "{`index`:`20`,`value`:`RA`}<1", and I want it to become "`RA`<1". I don't think the replace function is sufficient as the index and value changes based on what I enter. Is there a way to do that?
assuming the required string is everything after the last :, except for the }, the original string can be sliced and edited as follows:
const string = "{`index`:`20`,`value`:`RA`}<1";
let newString = string.slice(string.lastIndexOf(":")+1).split("}").join("");
console.log(newString);
This will always append everything after the final } to everthing before it but after the last :, regardless of the earlier content.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to trim my string and want to get only the last charecter after '/' , how to do split and trim to get last char ,
eg : '/projects/100'
i want 100 in return after trim
Just use
'/projects/100'.split('/')[2]
var string = '/projects/100';
var splits = string.split('/');
console.log(splits[splits.length - 1])
Try this:
String example = "/projects/100";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
You can use a regular expression
const str = '/projects/100'
const [res] = str.match(/[0-9]+$/g)
console.log(res)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to ensure the language is always extracted from a string, only before the 2nd dash (-)
So
en-AU-Option-A
becomes
en-AU
Is this possible with a single line of Javascript?
Split takes a second argument to specify no. of element desired. so in this case you can specify 2 and it will split upto 2nd - and than join -
let str = `en-AU-Option-A`
console.log(str.split('-',2).join('-'))
Of course it is possible. This is not using regex.
let mystr = 'en-AU-Option-A'
console.log(mystr.split('-').slice(0, 2).join('-'))
Here is a regex solution
var regex = /^([a-z]+|[A-Z]+)\-([a-z]+|[A-Z]+)/g;
var str = 'en-AU-Option-A';
console.log(str.match(regex)) // ["en-AU"]
Try This:
var patt = /[^-]*-[^-]*/ig ;
console.log( patt.exec( 'en-AU-Option-A' )[0] )
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to replace commas with a space like this:
If I have 1 comma, like : , I want replace it with a space only.
Or in other cases, I have 2 or more consecutive commas like : ,, or ,,, or ,,,,
I also want to replace the consecutive commas with one space only.
EX: var x = "black,white,,red,,,,blue";
I want to get "black white red blue";
How can I do it.
Thank you very much.
This can be done using JavaScript's replace method using a regular expression:
var x = "black,white,,red,,,,blue";
x = x.replace(/[, ]+/g, " ");
Demo
Try this:
var x = "black,white,,red,,,,blue";
var n = x.replace(/,+/g, ' ');
alert(n);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1 , 11, 1111);
How can I do it?
split() on - and pop() of the last value
string.split('-').pop()
Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();
Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"