A regex phone reformatting for JS [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 5 years ago.
Improve this question
I have phone number like: 89227611508 and i need to reformat this with regex(JS) into +8 922 761-15-08. I'm new in regex and can't get it done. Can anyone provide a simple solution?

Use:
"89227611508".replace(/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/, "+$1 $2 $3-$4-$5");

You need to use a regular expression to grab the groups of numbers then concatenate the number groups into a formatted string.
let number = '89227611508';
console.log(formatNumber(number));
function formatNumber(number) {
let groups = number.match(/^(\d)(\d{3})(\d{3})(\d{2})(\d{2})$/);
return '+' + groups[1] + ' ' + groups[2] + ' ' + groups[3] + '-' + groups[4] + '-' + groups[5];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Related

String with character repetition [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
Can someone help me on this one? Given a string, I have to return a string in which each character (case-sensitive) is repeated once.
doubleChar("String") ==> "SSttrriinngg"
doubleChar("Hello World") ==> "HHeelllloo WWoorrlldd"
doubleChar("1234!_ ") ==> "11223344!!__ "
function doubleChar(str) {
}
You can use repeat() method for this like:
function doubleChar(str) {
return [...str].map(s => s.repeat(2)).join('')
}
console.log(doubleChar("String"))
console.log(doubleChar("Hello World"))
console.log(doubleChar("1234!_ "))
Try this:
const str = 'hello'
let arr = str.split('')
const double = arr.map(i => i += i).join('')
console.log(double)

Need help to understand Vue.js function [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 3 years ago.
Improve this question
I have an assignment to make calculator using vue.js.
Much of it works, I'm confused about how to implement the '=' function.
I found this piece of code in an article that does something similar but I don't understand what it does. Can someone explain it?
https://scotch.io/tutorials/build-a-scientific-calculator-with-vuejs
I found this Piece of code:
if ((app.current).indexOf("^") > -1) {
var base = (app.current).slice(0, (app.current).indexOf("^"));
var exponent = (app.current).slice((app.current).indexOf("^") + 1);
app.current = eval("Math.pow(" + base + "," + exponent + ")");
} else {
app.current =app.current
}
Can someone please explain what the above function does, line-by-line?
// checks if app.current have '^' by getting the index
// if the method indexOf doesn't find anything it return -1
if (app.current.indexOf('^') > -1) {
// gets the first element till the index where it found '^'
var base = app.current.slice(0, app.current.indexOf('^'));
// gets the number after the 'ˆ'
var exponent = app.current.slice(app.current.indexOf('^') + 1);
// eval is evil
// it gets the string and transfoms into valid code
// wich means to do the operation
app.current = eval('Math.pow(' + base + ',' + exponent + ')');
} else {
// if it doesn't find the index it keeps the same value
app.current = app.current;
}
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

find all substrings between ${ and } 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 6 years ago.
Improve this question
i have collections of text with some text between ${ and } like "this is ${test} string ${like}". How can I extract all there strings. Output : test,like
try
match(/{[\w\d]+}/g);
example
"{asdas}32323{234}".match(/{[\w\d]+}/g); //outputs ["{asdas}", "{234}"]
It will return with { and } with the matches which you can remove from the resultset by
"{asdas}32323{234}".match(/{[\w\d]+}/g).map(function(value){return value.substring(1, value.length-1)}); //outputs ["asdas", "234"]
you can try:
"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)})
//["test", "like"]
Try this
var str = "this is ${test} string ${like}";
var txt = str.match(/{[\w\d]+}/g);
for(var i=0; i < txt.length; i++) {
txt[i] = txt[i].replace(/[{}]/g, '');
alert(txt[i]);
}

Syntax error js object to json [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
Hello i'm using Backbone here i have an issue, my 'history' attribute will be
history = history + ' * ' + 'Attribute "used" modified from ' + !this.changed.used + ' to ' + this.changed.used + ' by ' + LOGGED_USER.get("fisrtName") + " " + LOGGED_USER.get("name") + ' the ' + (new Date());
But when i do
this.set("history", history);
var json = JSON.stringify(this.toJSON())
the json returned is kind of corrupted with \
Any idea ?
Because the " needs to be escaped in strings, it is not corrupted.
{ "key" : "I am \"valid\"" }

Combine to get new variable name [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
I need to get this: $('#anchor')
Where anc hor are variables themselves.
var firstPart = "anc"
var secondPart = "hor"
How do I do that? + to the question (sorry for that) if I have this structure:
<a id="233"></a><li>qwerty</li>
how do I addClass to this exact li that comes after #233?
First part:
$('#' + firstPart + secondPart); // $('#anchor')
Second part:
$('#123 + li').addClass('myclass');
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
Answer1
var firstPart = "anc"
var secondPart = "hor"
var selector = "#" + firstPart + secondPart;
Then you can call it by $(selector) .
Answer 2:
$(function(){
$('#233 + li').addClass('ownclass');
});
Check http://jsfiddle.net/alaminopu/U2q6E/ for Answer 2.
'#anchor' is just a string. You use standard string concatination.
var str = '#' + firstPart + secondPart;
$(str)

Categories

Resources