single regex to capitalize first letter and replace dot - javascript

Trying out with a regex for simple problem. My input string is
firstname.ab
And am trying to output it as,
Firstname AB
So the main aim is to capitalize the first letter of the string and replace the dot with space. So chose to write two regex to solve.
First One : To replace dot with space /\./g
Second One : To capitalize the first letter /\b\w/g
And my question is, Can we do both operation with a single regex ?
Thanks in advance !!

You can use a callback function inside the replace:
var str = 'firstname.ab';
var result = str.replace(/^([a-zA-Z])(.*)\.([^.]+)$/, function (match, grp1, grp2, grp3, offset, s) {
return grp1.toUpperCase() + grp2 + " " + grp3.toUpperCase();
});
alert(result);
The grp1, grp2 and grp3 represent the capturing groups in the callback function. grp1 is a leading letter ([a-zA-Z]). Then we capturing any number of character other than newline ((.*) - if you have linebreaks, use [\s\S]*). And then comes the literal dot \. that we do not capture since we want to replace it with a space. And lastly, the ([^.]+$) regex will match and the capture all the remaining substring containing 1 or more characters other then a literal dot till the end.
We can use capturing groups to re-build the input string this way.

var $input = $('#input'),
value = $input.val(),
value = value.split( '.' );
value[0] = value[0].charAt( 0 ).toUpperCase() + value[0].substr(1),
value[1] = value[1].toUpperCase(),
value = value.join( ' ' );
$input.val( value );
It would be much easier if you simply split the value, process the string in the array, and join them back.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="first.ab" id="input">

Related

Masking phone number with regex in javascript

My application has a specific phone number format which looks like 999.111.222, which I have a regex pattern to mask it on front-end:
/[0-9]{3}\.[0-9]{3}\.([0-9]{3})/
But recently, the format was changed to allow the middle three digits to have one less digit, so now both 999.11.222 and 999.111.222 match. How can I change my regex accordingly?
"999.111.222".replace(/[0-9]{3}\.[0-9]{3}\.([0-9]{3})/, '<div>xxx.xxx.$1</div>')
expected output:
"999.111.222" // xxx.xxx.222
"999.11.222" // xxx.xx.222
Replace {3} with {2,3} to match two or three digits.
/[0-9]{3}\.[0-9]{2,3}\.([0-9]{3})/
For reference see e.g. MDN
Use
console.log(
"999.11.222".replace(/[0-9]{3}\.([0-9]{2,3})\.([0-9]{3})/, function ($0, $1, $2)
{ return '<div>xxx.' + $1.replace(/\d/g, 'x') + '.' + $2 + '</div>'; })
)
The ([0-9]{2,3}) first capturing group will match 2 or 3 digits, and in the callback method used as the replacement argument, all the digits from th first group are replaced with x.
You may further customize the pattern for the first set of digits, too.
In fact, you should change not only your regex but also your callback replace function:
const regex = /[0-9]{3}\.([0-9]{2,3})\.([0-9]{3})/;
const cbFn = (all, g1, g2) =>`<div>xxx.xx${(g1.length === 3 ? 'x' : '')}.${g2}</div>`;
const a = "999.11.222".replace(regex, cbFn);
const b = "999.111.222".replace(regex, cbFn);
console.log(a, b);
To change regex you could add a term with {2,3} quantifier, as already suggested, and create a new group. Then, in replace cb function, you can use length to know if you must put a new x.

Adding space after dot but not in URL

I have a text that has sentences that may not have space after a dot like:
See also vadding.Constructions on this term abound.
How can I add a space after a dot that is not before the domain name? The text may have URLs like:
See also vadding.Constructions on this term abound. http://example.com/foo/bar
Match and capture an URL and just match all other dots to replace with a dot+space:
var re = /((?:https?|ftps?):\/\/\S+)|\.(?!\s)/g;
var str = 'See also vadding.Constructions on this term abound.\nSee also vadding.Constructions on this term abound. http://example.com/foo/bar';
var result = str.replace(re, function(m, g1) {
return g1 ? g1 : ". ";
});
document.body.innerHTML = "<pre>" + result + "</pre>";
The URL regex - (?:https?|ftps?):\/\/\S+ - matches http or https or ftp, ftps, then :// and 1+ non-whitespaces (\S+). It is one of the basic ones, you can use a more complex one that you can easily find on SO. E.g. see What is a good regular expression to match a URL?.
The approach in more detail:
The ((?:https?|ftps?):\/\/\S+)|\.(?!\s) regex has 2 alternatives: the URL matching part (described above), or (|) the dot matching part (\.(?!\s)).
NOTE that (?!\s) is a negative lookahead that allows matching a dot that is NOT followed with a whitespace.
When we run string.replace() we can specify an anonymous callback function as the second argument and pass the match and group arguments to it. So, here, we have 1 match value (m) and 1 capture group value g1 (the URL). If the URL was matched, g1 is not null. return g1 ? g1 : ". "; means we do not modify the group 1 if it was matched, and if it was not, we matched a standalone dot, thus, we replace with with . .
You can try using RegExp /(\.)(?!=[a-z]{2}\/|[a-z]{3}\/|\s+|$)/g to match . character if not followed by two or three lowercase letters or space character
"See also vadding.Constructions on this term abound. http://example.com/foo/bar"
.replace(/(\.)(?!=[a-z]{2}\/|[a-z]{3}\/|\s+|$)/g, "$1 ")
Using idea from #MarcelKohls
var text = "See also vadding.Constructions on this term abound. http://example.com/foo/bar";
var url_re = /(\bhttps?:\/\/(?:(?:(?!&[^;]+;)|(?=&))[^\s"'<>\]\[)])+\b)/gi;
text = text.split(url_re).map(function(text) {
if (text.match(url_re)) {
return text;
} else {
return text.replace(/\.([^ ])/g, '. $1');
}
}).join('');
document.body.innerHTML = '<pre>' + text + '</pre>';
Use this pattern:
/\.(?! )((?:ftp|http)[^ ]+)?/g
Online Demo

match numbers without a prefix

I need help with regular expression.
Using javascript I am going through each line of a text file and I want to replace any match of [0-9]{6,9} with a '*', but, I don't want to replace numbers with prefix 100. So, a number like 1110022 should be replaced (matched), but 1004567 should not (no match).
I need a single expression that will do the trick (just the matching part). I can’t use ^ or $ because the number can appear in the middle of the line.
I have tried (?!100)[0-9]{6,9}, but it doesn't work.
More examples:
Don't match: 10012345
Match: 1045677
Don't match:
1004567
Don't match: num="10034567" test
Match just the middle number in the line: num="10048876" 1200476, 1008888
Thanks
You need to use a leading word boundary to check if a number starts with some specific digit sequence:
\b(?!100)\d{6,9}
See the regex demo
Here, the 100 is checked right after a word boundary, not inside a number.
If you need to replace the matches with just a single asterisk, just use the "*" as a replacement string (see snippet right below).
var re = /\b(?!100)\d{6,9}/g;
var str = 'Don\'t match: 10012345\n\nMatch: 1045677\n\nDon\'t match:\n\n1004567\n\nDon\'t match: num="10034567" test\n\nMatch just the middle number in the line: num="10048876" 1200476, 1008888';
document.getElementById("r").innerHTML = "<pre>" + str.replace(re, '*') + "</pre>";
<div id="r"/>
Or, if you need to replace each digit with *, you need to use a callback function inside a replace:
String.prototype.repeat = function (n, d) {
return --n ? this + (d || '') + this.repeat(n, d) : '' + this
};
var re = /\b(?!100)\d{6,9}/g;
var str = '123456789012 \nDon\'t match: 10012345\n\nMatch: 1045677\n\nDon\'t match:\n\n1004567\n\nDon\'t match: num="10034567" test\n\nMatch just the middle number in the line: num="10048876" 1200476, 1008888';
document.getElementById("r").innerHTML = "<pre>" + str.replace(re, function(m) { return "*".repeat(m.length); }) + "</pre>";
<div id="r"/>
The repeat function is borrowed from BitOfUniverse's answer.

Javascript - How to join two capitalize first letter of word scripts

I have an Acrobat form with some text fields with multiline on. My goal is to convert to uppercase the first letter of any sentence (look for dots) and also the first letter of any new line (after return has been pressed).
I can run each transformation separately, but do not know how to run them together.
To capitalize sentences I use the following code as custom convalidation :
// make an array split at dot
var aInput = event.value.split(". ");
var sCharacter = '';
var sWord='';
// for each element of word array, capitalize the first letter
for(i = 0; i <aInput.length; i++)
{
aInput[i] = aInput[i].substr(0, 1).toUpperCase() + aInput[i].substr(1) .toLowerCase();
}
// rebuild input string with modified words with dots
event.value = aInput.join('. ');
To capitalize new lines I replace ". " with "\r".
Thanks in advance for any help.
You can get the first character of each sentence with RegExp :
event.value = event.value.replace(/.+?[\.\?\!](\s|$)/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
Demo : http://jsfiddle.net/00kzc370/
Regular Expression explained :
/.+?[\.\?\!](\s|$)/g is a regular expression.
.+?[\.\?\!](\s|$) is a pattern (to be used in a search) that match sentences ended by ., ? or ! and followed by a whitespace character.
g is a modifier. (Perform a global match (find all matches rather than stopping after the first match)).
Source : http://www.w3schools.com/jsref/jsref_obj_regexp.asp

How to get the last value of a string?

How can i control in java script, wheter the last character in a string is a ", " and how can i delete the last character, if it is a ", "?
var stringToControl = "ABC, XYZ, OOP, "
"Comma followed by any number of white space characters at the end of a string" can be expressed as a regular expression: /,\s*$/.
So to get rid of it you can replace it with an empty string:
stringToControl = stringToControl.replace(/,\s*$/,'');
Try this:
if (stringToControl.slice(-2) === ', ') {
stringToControl = stringToControl.slice(0, -2);
}
slice method returns part of a string. First argument is the start position and optional second argument - the end position. If you don't pass second arguments, slice will return part from start argument to the end of the string. If you pass negative value, it means the position from the end (so .slice(-2) means 2 last characters).
Here is an article on MDN
Another approach is to use RegExp:
stringToControl = stringToControl.replace(/, $/, '');
use slice
var temp = "abcd,aa,a,as,c,d, ".trim();
if(temp.endsWith(','))
temp = temp.slice(0,-1);
temp

Categories

Resources