Remove leading + or 00 from number using regex [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 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);

Related

JavaScript Regex Expression needed for split method [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 1 year ago.
Improve this question
I'm trying to remove characters after numbers and needs to include .pdf extension.
I cannot use index since the file name varies on dates and names.
ex: My File Name : Mar 1316 and A - sec.pdf
required output file name: Mar1316.pdf
can anyone help with the regex and split method?
If there can be only one sequence of numbers or you want until the last sequence you can use this regex: (.*[1-9]+)
.*: any character any number of time
[1-9]+: 1 number or more
const str = "Mar 1316 and A - sec.pdf";
const match = /.*[1-9]+/.exec(str);
if (match) {
// will equal 'Mar 1316.pdf'
const newStr = str.slice(0, match[0].length) + '.pdf';
}
I don't think you can do this with JS regex, simply because you need both lookbehind and lookahead in the same regex, which I believe you cannot do.
/\s+|(?<=\d).*(?=\.)/

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 everything from string after the second - with a single line in Javascript [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 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] )

Full-width Numbers convert to half-width Numbers in jQuery / JS [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 2 years ago.
Improve this question
A would like to convert Full-width Numbers (e.g. 123) to half-width Numbers (e.g. 123). I found code do this in PHP but not in JS. Could anyone helps? Thanks.
function fullWidthNumConvert(fullWidthNum){
// Magic here....
....
return halfWidthNum;
}
Do a string .replace(), using a regular expression to match the characters in question. The callback in .replace()'s second argument can get the character code of the matched character and subtract from that to get the character code of the standard digit, then convert that back to a string.
function fullWidthNumConvert(fullWidthNum){
return fullWidthNum.replace(/[\uFF10-\uFF19]/g, function(m) {
return String.fromCharCode(m.charCodeAt(0) - 0xfee0);
});
}
console.log(fullWidthNumConvert("0123456789"));
console.log(fullWidthNumConvert("Or in the middle of other text: 123. Thank you."));
Using String Normalize MDN with "NFKC" as the Unicode Normalization Form
const str = "150721";
console.log(str.normalize('NFKC')); // 150721
https://www.unicode.org/charts/normalization/chart_Number-Decimal.html

Need a regular expression to match a substring n times [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 9 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
The regex .*{n} will match any single character n times, but I need to match any single substring n times.
How do I do that?
To match the substring "foo" 3 times (for example "foofoofoo"), you could use the following:
(foo){3}
Or with a non-capturing group:
(?:foo){3}
As a side note, .*{n} wouldn't do what you think it does. The . will match any character, .* will match any number of any characters, and .*{n} will vary depending on the implementation but it will either be an invalid regex, be equivalent to .*, or match any number of any characters followed by the literal string '{n}'.
Try
(your sub string here){n}
e.g.
(cats){4}
try
/(\w+)\1{n-1}/
Example:
"abcbcbca".match(/(\w+)\1{2}/) if you wish to find bc being repeated 3 times.
If you are trying to match a given string repeated n times, just do (string){n}.

Categories

Resources