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);
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 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 4 years ago.
Improve this question
I need to remove + or 00 from the beginning of a number in case they exist. So a number like +37253783478 would output 37253783478 and 0037253783478 would output 37253783478. What would the regex look like that matches this pattern?
EDIT: I've managed to remove the leading zeros using ^0+ but I can't figure out how to match both cases.
If I understand the requirement, the following will match both cases. Essentially, what you need to do is use the regex or operator |.
The following will remove all leading 0s
str.replace(/(^0+|^\+)/,'')
But if you just need to remove exactly two leading 0s, use this:
str.replace(/(^00|^\+)/,'')
And here it is in action on your examples:
let nums = ['+37253783478', '0037253783478', '0037253780478', '375378+0478'];
let replaced = nums.map(num => num.replace(/(^0+|^\+)/,''));
console.log(replaced);
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
Say I have a String
Thi__(1)__s i__(2)__s a test__(5)__ mess__(4)__age __(6)__
I want to replace the numbers in between the pattern __()__ by that number-1, that is, 2 with 1, 3 with 2 and so on. And there is a condition, that number should be greater than 3.
So my final string will look like
Thi__(1)__s i__(2)__s a test__(4)__ mess__(3)__age __(5)__
I know how to make a logic for that but as I am new to Javascript/Jquery I am looking for a better way.
Any help would be appreciated.
var str = "Thi__(1)__s i__(2)__s a test__(5)__ mess__(4)__age __(6)__";
var replaced = str.replace(/(__\()(\d+)(\)__)/g, function(_, left, val, right){ //replace all (digits)
val = +val; //convert to number
return left + (val > 3 ? --val: val) + right; //use whatever convert logic you need
});
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 9 years ago.
Improve this question
What is the difference between regular expression Modifier 'i' and 'm'.
Example:
var str = "Visit W3Schools";
var patt1 = /w3schools/i;
document.write(str.match(patt1));
var str = "Visit W3Schools";
var patt1 = /w3schools/m;
document.write(str.match(patt1));
Um, it's kinda hard to see what you're asking, but I think what you want is
new RegExp('1.*'+str,'i');
The period (.) matches any character and the * matches any character zero or more times. I'm not 100% on the syntax of that regex in javascript, but that should be a minor issue. Maybe a str.toString() is missing or something?