Javascript only read certain char from input [duplicate] - javascript

This question already has answers here:
How to return part of string before a certain character?
(5 answers)
Closed 6 years ago.
var name = prompt("Enter Name,Barry");
document.write(name);
I want to do exactly this, but only print the first three letters of the 'name'

var name = prompt("Enter Name,Barry").slice(0, 3);
document.write(name)
Try this out it may help.

Related

How to get URLinformation after question mark? [duplicate]

This question already has answers here:
how to get url value after question mark in javascript
(2 answers)
Closed 3 years ago.
I have this simple code:
Subscribe today
when the user press on the a tag it takes him to the page subsc.php?English.
My question is how can i get the information after the ? (which is "English" in this example)?
You can try with Regex Positive Lookbehind /(?<=\?).$*/.
var url = '/subsc.php?English';
var r = url.match(/(?<=\?).*$/)[0];
console.log(r);

Javascript split number and string and keep both [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks

How to format string in JS? [duplicate]

This question already has answers here:
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 6 years ago.
I would like to have the result :
28,12,2016
From this string "28/12/2016"
I tried :
("28/12/2016").replace('/',',');
==>"28,12/2016"
I don't know how to delete the second /and the " "
use split and join method
var a="28/12/2016";
var ans=a.split("/").join(",");
console.log(ans);

format phone no is us style eg: (xxx)xxx-xxx [duplicate]

This question already has answers here:
Formatting the phone number
(3 answers)
Closed 6 years ago.
How do I format plain 10 digit phone no. to us style (xxx)-xxx-xxxx using javascript regular expression.
This should do it: ^(\d{3})(\d{3})(\d{4})$
Here is a working example:
var testNumber = '1234567890';
var regex = /^(\d{3})(\d{3})(\d{4})$/;
var sub = '($1)-$2-$3';
var usNumber = testNumber.replace(regex, sub);
alert(usNumber);

How do I get the last character with jquery [duplicate]

This question already has answers here:
How to get the last character of a string?
(15 answers)
Closed 8 years ago.
I have a string I am trying to get the last character with jquery. How would I do that?
var stringTemp = "fieldNameWithIndex_1";
stringTemp.charAt(stringTemp.length-1);
or
stringTemp.slice(-1);
Use .split():
stringTemp.split('_')[1]

Categories

Resources