I have a input field that should accept only these format (d- digit, c- character):
d.d
d.d.d
d.d.c
My code:
var format1 = /[0-9]{1,}\.[0-9]{1,}/g; // d.d
var format2 = /[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}/g; // d.d.d
var format3 = /[0-9]{1,}\.[0-9]{1,}\.[a-zA-Z]{1,}/g; // d.d.c
if (format1.test(input)) {
format1Con = true;
}
if (format2.test(input)) {
format2Con = true;
}
if (format3.test(input)) {
format3Con = true;
}
This code allows some wrong type values.
For example -
1.2.3.33,
1.2.ccccc (Here only one character should be aaccepted)
Please help with exact regular expression for my field format.
You can use
/^\d\.\d+(?:\.(?:\d|[a-z]))?$/i
See demo
The regex matches...
^ - Beginning of a string
\d - a digit
\. - a literal dot
\d+ - 1 or more digits
(?:\.(?:\d|[a-z]))? - an optional group matching...
\. - literal dot and...
(?:\d|[a-z]) - either a single digit or letters from [a-zA-Z] range (since i modifier is used)
$ - End of string
var re = /^\d\.\d+(?:\.(?:\d|[a-z]))?$/;
document.write('1.2.3.33: ' + re.test('1.2.3.33') + "<br/>");
document.write('1.2.ccccc: ' + re.test('1.2.ccccc') + "<br/>");
document.write('1.2: ' + re.test('1.2') + "<br/>");
document.write('1.22: ' + re.test('1.22') + "<br/>");
document.write('1.22.3: ' + re.test('1.22.3') + "<br/>");
document.write('1.2.3: ' + re.test('1.2.3') + "<br/>");
document.write('1.2.x: ' + re.test('1.2.x') + "<br/>");
You can modify your RegEXP like this:
var format1 = /^[0-9]{1,}\.[0-9]{1,}$/g; // d.d
var format2 = /^[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}$/g; // d.d.d
var format3 = /^[0-9]{1,}\.[0-9]{1,}\.[a-zA-Z]{1,}$/g; // d.d.c
Where ^ matches the start of the string and $ matches the end of the string.
Related
var str = "sdhdhh#gmail.com"; // true but coming false
var str1 = "sdhdhh#gmail.co.uk";
var str2 = "sdhdhh#gmail.org";
var str3 = "sdhdhh#gmail.org.uk";
var patt = new RegExp("[a-z0-9._%+-]+#[a-z0-9.-]+?[\.com]?[\.org]?[\.co.uk]?[\.org.uk]$");
console.log( str + " is " + patt.test(str));
console.log( str1 + " is " + patt.test(str1));
console.log( str2 + " is " + patt.test(str2));
console.log( str3 + " is " + patt.test(str3));
Can anyone tell me what is the mistake, my .com example is not working properly
You need
A grouping construct instead of character classes
A regex literal notation so that you do not have to double escape special chars
The ^ anchor at the start of the pattern since you need both ^ and $ to make the pattern match the entire string.
So you need to use
var patt = /^[a-z0-9._%+-]+#[a-z0-9.-]+?(?:\.com|\.org|\.co\.uk|\.org\.uk)$/;
See the regex demo.
If you need to make it case insensitive, add i flag,
var patt = /^[a-z0-9._%+-]+#[a-z0-9.-]+?(?:\.com|\.org|\.co\.uk|\.org\.uk)$/i;
Im trying to replace a character at a specific indexOf to uppercase.
My string is a surname plus the first letter in the last name,
looking like this: "lovisa t".
I check the position with this and it gives me the right place in the string. So the second gives me 8(in this case).
first = texten.indexOf(" ");
second = texten.indexOf(" ", first + 1);
And with this I replace the first letter to uppercase.
var name = texten.substring(0, second);
name=name.replace(/^./, name[0].toUpperCase());
But how do I replace the character at "second" to uppercase?
I tested with
name=name.replace(/.$/, name[second].toUpperCase());
But it did´t work, so any input really appreciated, thanks.
Your error is the second letter isn't in position 8, but 7.
Also this second = texten.indexOf(" ", first + 1); gives -1, not 8, because you do not have a two spaces in your string.
If you know that the string is always in the format surname space oneLetter and you want to capitalize the first letter and the last letter you can simply do this:
var name = 'something s';
name = name[0].toUpperCase() + name.substring(1, name.length - 1) + name[name.length -1].toUpperCase();
console.log(name)
Here's a version that does exactly what your question title asks for: It uppercases a specific index in a string.
function upperCaseAt(str, i) {
return str.substr(0, i) + str.charAt(i).toUpperCase() + str.substr(i + 1);
}
var str = 'lovisa t';
var i = str.indexOf(' ');
console.log(upperCaseAt(str, i + 1));
However, if you want to look for specific patterns in the string, you don't need to deal with indices.
var str = 'lovisa t';
console.log(str.replace(/.$/, function (m0) { return m0.toUpperCase(); }));
This version uses a regex to find the last character in a string and a replacement function to uppercase the match.
var str = 'lovisa t';
console.log(str.replace(/ [a-z]/, function (m0) { return m0.toUpperCase(); }));
This version is similar but instead of looking for the last character, it looks for a space followed by a lowercase letter.
var str = 'lovisa t';
console.log(str.replace(/(?:^|\s)\S/g, function (m0) { return m0.toUpperCase(); }));
Finally, here we're looking for (and uppercasing) all non-space characters that are preceded by the beginning of the string or a space character; i.e. we're uppercasing the start of each (space-separated) word.
All can be done by regex replace.
"lovisa t".replace(/(^|\s)\w/g, s=>s.toUpperCase());
Try this one (if it will be helpfull, better move constants to other place, due performance issues(yes, regexp creation is not fast)):
function normalize(str){
var LOW_DASH = /\_/g;
var NORMAL_TEXT_REGEXP = /([a-z])([A-Z])/g;
if(!str)str = '';
if(str.indexOf('_') > -1) {
str = str.replace(LOW_DASH, ' ');
}
if(str.match(NORMAL_TEXT_REGEXP)) {
str = str.replace(NORMAL_TEXT_REGEXP, '$1 $2');
}
if(str.indexOf(' ') > -1) {
var p = str.split(' ');
var out = '';
for (var i = 0; i < p.length; i++) {
if (!p[i])continue;
out += p[i].charAt(0).toUpperCase() + p[i].substring(1) + (i !== p.length - 1 ? ' ' : '');
}
return out;
} else {
return str.charAt(0).toUpperCase() + str.substring(1);
}
}
console.log(normalize('firstLast'));//First Last
console.log(normalize('first last'));//First Last
console.log(normalize('first_last'));//First Last
I have a string like this:
var str = "this is test
1. this is test
2. this is test
3. this is test
this is test
1. this test
2. this is test
this is test";
Also I have this regex:
/^[\s\S]*(?:^|\r?\n)\s*(\d+)(?![\s\S]*(\r?\n){2})/m
This capturing group $1 returns 2 from above string.
Now I have a position number: 65 and I want to apply that regex in this range of the string: [0 - 65]. (So I have to get 3 instead of 2). In general I want to limit that string from first to a specific position and then apply that regex on that range. How can I do that?
The simplest way is to apply it to just that substring:
var match = /^[\s\S]*(?:^|\r?\n)\s*(\d+)(?![\s\S]*(\r?\n){2})/m.exec(str.substring(0, 65));
// Note ----------------------------------------------------------------^^^^^^^^^^^^^^^^^
Example:
var str = "this is test\n1. this is test\n2. this is test\n3. this is test\nthis is test\n1. this test \n2. this is test\nthis is test";
var match = /^[\s\S]*(?:^|\r?\n)\s*(\d+)(?![\s\S]*(\r?\n){2})/m.exec(str.substring(0, 65));
// Note ----------------------------------------------------------------^^^^^^^^^^^^^^^^^
document.body.innerHTML = match ? "First capture: [" + match[1] + "]" : "(no match)";
Maybe such a build can help (source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)
var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) {
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
Assume that there is a string like this:
var content = "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20";
I want to add <br /> after every 5 dots.
So, the result should be:
1.2.3.4.5.<br />
6.7.8.9.10.<br />
11.12.13.14.15.<br />
16.17.18.19.20.<br />
I want to do this without a for loop. Is it possible with just regex?
i'm doing this with this code;
regenerate:function(content,call){
var data2;
var brbr = content.replace(/[\u0250-\ue007]/g, '').match(/(\r\n)/g);
if (brbr !== "") {
data2 = content.replace(/[\u0250-\ue007]/g, '').replace(/(\r\n)/gm, "<br><br>");
} else {
data2 = content.replace(/[\u0250-\ue007]/g, '');
}
var dataArr = data2.split(".");
for (var y = 10; y < dataArr.length - 10; y += 10) {
var dataArrSpecific1 = dataArr[y] + ".";
var dataArrSpecific2 = dataArr[y] + ".<br>";
var dataArrSpecificBosluk = dataArr[y + 1];
var data3 = data2.replace(new RegExp(dataArrSpecific1.replace(/[\u0250-\ue007]/g, ''), "g"), "" + dataArrSpecific2.replace(/[\u0250-\ue007]/g, '') + "");
data3 = data3.replace(new RegExp(dataArrSpecificBosluk.replace(/[\u0250-\ue007]/g, ''), "g"), " " + dataArrSpecificBosluk.replace(/[\u0250-\ue007]/g, '') + "");
data2 = data3;
}
call(data2.replace(/[\u0250-\ue007]/g, ''));
}
Actually , i want to refactoring this code
Working bin:http://jsbin.com/dikifipelo/1/
var string = "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20." ;
string = string.replace(/(([^\.]+\.){5})/g, "$1<br/>");
Works with any type and length of characters between the dots.
Explanation:
The pattern /(([^.]+.){5})/g can be broken down as such:
[^\.] - any character that is not a dot
[^\.]+ - any character that is not a dot, one or more times
[^\.]+\. - any character that is not a dot, one or more times, followed by a dot
([^\.]+\.){5} - any character....dot, appearing five times
(([^\.]+\.){5}) - any...five times, capture this (all round brackets capture unless told not to, with a ?: as the first thing inside them)
the /g/ flag makes it so that the whole string is matched - ie, all matches are found
"$1" represents the results of the first group (or bracket)
so, the replace function finds all instances of the pattern in the string, and replaces them with the match itself + a line break (br).
Once you learn regular expressions, life is never the same.
function convertToValidPhoneNumber(text) {
var result = [];
text = text.replace(/^\d{2}-?\d{3}-?\d{3}-?\d{3}$/, "");
while (text.length >= 6){
result.push(text.substring(0, 3));
text = text.substring(3);
}
if (text.length > 0) result.push(text);
return result.join("-");
}
I test this function against 35200123456785 input string.
It gives me like a number like 352-001-234-56785 but I want 35200-12345678-5.
What do I need to do to fix this?
You can use this updated function that uses ^(?=[0-9]{11})([0-9]{5})([0-9]{8})([0-9])$ regex:
^ - String start
(?=[0-9]{11}) - Ensures there are 11 digits in the phone number
([0-9]{5}) - First group capturing 5 digits
([0-9]{8}) - Second group capturing 8 digits
([0-9]) - Third group capturing 1 digit
$ - String end
Replacement string - "$1-$2-$3" - uses back-references to those capturing groups in the regex by numbers and adds hyphens where you need them to be.
In case you have hyphens inside the input string, you should remove them before.
function convertToValidPhoneNumber(text) {
return text = text.replace(/-/g,"").replace(/^(?=[0-9]{11})([0-9]{5})([0-9]{8})([0-9])$/, "$1-$2-$3");
}
document.getElementById("res").innerHTML = convertToValidPhoneNumber("35200123456785") + " and " + convertToValidPhoneNumber("35-200-123-456-785");
<div id="res"/>
number = this.state.number;
var expression = /(\D+)/g;
var npa = "";
var nxx = "";
var last4 = "";
number = number.toString().replace(expression, "");
npa = number.substr(0, 3);
nxx = number.substr(3, 3);
last4 = number.substr(6, 4);
number = npa + "-" + nxx + "-" + last4