Convert JavaScript string format into another [closed] - javascript

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 have a mobile no like (408) 931-4377 and need to create a format like (000) 000-0000 for validating other mobile nos
Please suggest how this can be achieved?

Use String.replace in javascript
Reference
const number = "(408) 931-4377";
console.log(number.replace(/[0-9]/g, "0"));

Use Regular Expressions for this Validation.
const number = "(408) 931-4377";
const mobilecheck = RegEx(/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/);
if(mobilecheck.test(number)){
// do something
}
else{
// do something
}```

Related

How to Remove two consecutive double quotes from array after split [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
I have a requirement where i will get the input from the user. He can enter
"features1,feature2",feature3,"feature4"
something like this. I'm using this
let arr: any = message.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)
to split the string and i'm getting result like
[""feature1,feature2"","feature3",""feature4""]
but i want the result like this
["feature1,feature2","feature3","feature4"]
I don't thing what you're trying to achieve is possible only using split function.
After you split, loop over the error and replace " with `` (empty).
const message = `"features1,feature2",feature3,"feature4"`;
let arr = message.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
arr = arr.map(el=>el.replace(/"/g,''));
console.log(arr);

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]

String to integer conversion using javascript [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 7 years ago.
Improve this question
I have a two textbox and i want to convert string value such as "apple" and "cat" into appropriate integer value using javascript.
Simple...
var myInt = "cat";
var converted = parseInt(myInt, 10);
console.log(converted);
>>NaN

Regular express to match `command--o1--o2--o3` [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 7 years ago.
Improve this question
I have a string like command--o1--o2--o3 ( command,o1,o2,o3 are arbitrary words)
And I want get [o1, o2, o3] though a Regular Expression(Not a array operation or other ways. JUST only use Regular Expression).
Is there any idea to accomplish this !?
If you're using JavaScript, and assuming you want all strings after a --, you may do
var things = str.split(/--/).slice(1)
If you just want to get the 2 characters words following --, then you may use
var things = str.match(/--\w\w/g).map(function(s){ return s.slice(2) })

Regex String URL [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.
Improve this question
Example
1. http://ad.ly/123411/http://blabalba.com/adaf
2. http://ad.ly/142145/http://blabal.com/adf34
how to replace front String http://ad.ly/1246721/ use js regex
into http://blabalbal.com/adaf
get this http://blabalbal.com/adaf
The regexp required is http:\/\/adf_.ly\/\d+\/(.+)
var str = 'http://adf_.ly/1246721/http://prefiles.com/files';
var re = /http:\/\/adf_.ly\/\d+\/(.+)/;
var newstr = re.exec(str)[1];
You can just take the part you're interested in using lookahead directive:
s = 'http://adf_.ly/123411/http://blabalba.com/adaf';
u = s.match(/(?!^)http:\/\/.+?$/)[0];
//=> http://blabalba.com/adaf

Categories

Resources