Javascript remove leading zeros and decimal points from string [closed] - javascript

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
searching for a regex to convert
0.015.000 -> 15.000
0.150.000 -> 150.00
015.000 -> 15.00

You could try the below string.replace function. Use ^ to tell the regex engine to do the matching operation from the start. By putting 0 and . inside a character class would match either 0 or dot.
string.replace(/^[0.]+/, "")
Example:
> "0.015.000".replace(/^[0.]+/, "")
'15.000'
> "0.150.000".replace(/^[0.]+/, "")
'150.000'
> "015.000".replace(/^[0.]+/, "")
'15.000'

Related

regex creation with multiple conditions [closed]

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 1 year ago.
Improve this question
i need a regex that fits these conditions
may contain letters and numbers. numbers are optional but must contain at least 1 letter
at least 2 characters
can contain ONLY the "-" character of special characters. this is optional
must begin with a letter
no whitespace
no turkish characters
How should i create a regex query according to these conditions?
I would create a function like so:
function isGood(string){
return /^[a-z][a-z0-9-]+$/i.test(string);
}
console.log(isGood('This should return false'))
console.log(isGood('#no'));
console.log(isGood('Nice'));
console.log(isGood('a'));
console.log(isGood('a!'));
console.log(isGood('great'));
console.log(isGood('This-should-also-pass-the-test'));

Remove leading + or 00 from number using regex [closed]

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);

Select >|< from string between quotes [closed]

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 4 years ago.
Improve this question
I have a string like this: *ngIf="thisThing > 0"
Using RegEx, I'd like to select/return ONLY the > or in some cases, <.
I have the following expression: (?<=")(?:.*?)(<|>)(?:.*?)(?=") but this still selects everything inside the quotes. I only want to match that >.
I've been testing it here: https://regex101.com/r/4KPfbT/1
I guess you're over-complexifying it. Why not this way:
\w*=".*([<>]).*"
It works, take a look:
console.log('*ngIf="thisThing > 0"'.match(/w*=".*([<>]).*"/)[1])
console.log('whatever="otherThing < 999"'.match(/w*=".*([<>]).*"/)[1])

Email Regex for Javascript Which not start with Number [closed]

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
^[a-zA-Z]\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,4}$
i had tried this regex for email but it allow following cases
123#mail.com
example.mail#mail.com
Here is the code which searches for the number at the start.
If number is matched it prints message in console.
let regex = /^[0-9]/;
let object = [{email:'123#mail.com'},{email:'example.mail#mail.com'}];
for(let i =0;i<object.length;i++){
if(object[i].email.match(regex)){
console.log('E-mail ',object[i].email,' is not valid.')
}
}
This is the used regex: ^[0-9]

Replace text in javascript, jquery [closed]

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);

Categories

Resources