regex to match dot and number follow the dot - javascript

I need to check a string in javascript if it contains dot followed by number (ex: xx.12) using regex
What is the correct regex for that.

You could try
let pat = /\.[0-9]/
if(pat.test(number))
{
//do something
}

correct regex to match your string is "xx\.12"
Backslash makes regex to take the next dot as a dot but not as anything.

Related

Regex catch from the hash sign "#" to the next white space

I have a script line this :
#type1 this is the text of the note
I've tried this bu didn't workout for me :
^\#([^\s]+)
I watch to catch type in other words I to get whats between the hash sign "#" and the next white space, excluding the hash "#" sign, and the string that I want to catch is alphanumeric string.
With the regex functionality provided by Javascript:
exec_result = /#(\w*)/.exec('#whatever string comes here');
I believe exec_result[1] should be the string you want.
The return value of exec() method could be found over here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
You're really close:
/^\#(\w+)\s/
The \w matches any letters or numbers (and underscores too). And the space should be outside the matching group since I guess you don't want to capture it.
To get an alphanumeric match (which will get you type1), instead of the negated character class [^\s] which matches not a whitespace character, you could use a character class and specify what you want to match like [A-Za-z0-9].
Then use a negative lookahead to assert what is on the right is not a non-whitespace char:
^#([A-Za-z0-9]+)(?!\S)
Regex demo
Your match is in the first capturing group. Note that you don't have to escape the \#
For example using the case insensitive flag /i
const regex = /^#([A-Za-z0-9]+)(?!\S)/i;
const str = `#type1 this is the text of the note`;
console.log(str.match(regex)[1]);
If you only want to match type, you might use:
^#([a-z]+)[a-z0-9]*(?!\S)
Regex demo
const regex = /^#([a-z]+)[a-z0-9]*(?!\S)/i;
const str = `#type1 this is the text of the note`;
console.log(str.match(regex)[1]);
I've figured it out.
/^\#([^\s]+)+(.*)$/

How to replace string between two string with the same length

I have an input string like this:
ABCDEFG[HIJKLMN]OPQRSTUVWXYZ
How can I replace each character in the string between the [] with an X (resulting in the same number of Xs as there were characters)?
For example, with the input above, I would like an output of:
ABCDEFG[XXXXXXX]OPQRSTUVWXYZ
I am using JavaScript's RegEx for this and would prefer if answers could be an implementation that does this using JavaScript's RegEx Replace function.
I am new to RegEx so please explain what you do and (if possible) link articles to where I can get further help.
Using replace() and passing the match to a function as parameter, and then Array(m.length).join("X") to generate the X's needed:
var str = "ABCDEFG[HIJKLMN]OPQRSTUVWXYZ"
str = str.replace(/\[[A-Z]*\]/g,(m)=>"["+Array(m.length-1).join("X")+"]")
console.log(str);
We could use also .* instead of [A-Z] in the regex to match any character.
About regular expressions there are thousands of resources, specifically in JavaScript, you could see Regular Expressions MDN but the best way to learn, in my opinion, is practicing, I find regex101 useful.
const str="ABCDEFG[HIJKLMN]OPQRSTUVWXYZ";
const run=str=>str.replace(/\[.*]/,(a,b,c)=>c=a.replace(/[^\[\]]/g,x=>x="X"));
console.log(run(str));
The first pattern /\[.*]/ is to select letters inside bracket [] and the second pattern /[^\[\]]/ is to replace the letters to "X"
We can observe that every individual letter you wish to match is followed by a series of zero or more non-'[' characters, until a ']' is found. This is quite simple to express in JavaScript-friendly regex:
/[A-Z](?=[^\[]*\])/g
regex101 example
(?= ) is a "positive lookahead assertion"; it peeks ahead of the current matching point, without consuming characters, to verify its contents are matched. In this case, "[^[]*]" matches exactly what I described above.
Now you can substitute each [A-Z] matched with a single 'X'.
You can use the following solution to replace a string between two square brackets:
const rxp = /\[.*?\]/g;
"ABCDEFG[HIJKLMN]OPQRSTUVWXYZ".replace(rxp, (x) => {
return x.replace(rxp, "X".repeat(x.length)-2);
});

Javascript function to remove leading dot

I have a javascript string which have a leading dot. I want to remove the leading dot using javascript replace function. I tried the following code.
var a = '.2.98»';
document.write(a.replace('/^(\.+)(.+)/',"$2"));
But this is not working. Any Idea?
The following replaces a dot in the beginning of a string with an empty string leaving the rest of the string untouched:
a.replace(/^\./, "")
Don't do regexes if you don't need to.
A simple charAt() and a substring() or a substr() (only if charAt(0) is .) will be enough.
Resources:
developer.mozilla.org: charAt()
developer.mozilla.org: substring()
developer.mozilla.org: substr()
Your regex is wrong.
var a = '.2.98»';
document.write(a.replace('/^\.(.+)/',"$1"));
You tried to match (.+) to the leading dot, but that doesn't work, you want \. instead.
Keep it simple:
if (a.charAt(0)=='.') {
document.write(a.substr(1));
} else {
document.write(a);
}

Javascript regex to check if a value begins with a number followed by anything else

How would i go about doing a regex to see if it begins with a number and any character can follow after. My current expression is
var validaddress = /^[0-9][A-Za-z0-9]+$/;
But this isn't the right way. Im new to this, help anyone?
If you need character(s) after the digit, try this:
var validaddress = /^[0-9].+$/;
If characters after the digit are optional, use this:
var validaddress = /^[0-9].*$/;
What you looking for is: var validaddress = /^\d.*$/;
\d - Matches any digit
.* - Matches any character except newline zero or more times.
Or replace .* with .+, if you are looking for at least 1 character.
Try /^[0-9]/ as the regular expression.
If it only needs to start with a number, I'd only check that...
when you say "any character follow" -- do you mean any alphanumeric character or just anything (i.e. including space, comma, slash etc)?
if it is the latter, how about this:
var validaddress = /^[0-9].+$/;
I suppose this should work
/^\d+.+$/
I would get rid of the $. Also, a '.' would suffice for "any character". This one works for me:
var validaddress = ^[0-9].+;
If you have a string of these values and you want to find each individually try this:
(^|(?<=\W))(\d\w*)
You can then do a loop through each match.
Regexr example
You could also use /^\d/ as the briefest approach.

how to check digits present in javascript string or not

I need a regex which check the string contains only A-Z, a-z and special characters but not digits i.e. (0-9).
Any help is appreciated.
You can try with this regex:
^[^\d]*$
And sample:
var str = 'test123';
if ( str.match(/^[^\d]*$/) ) {
alert('matches');
}
Simple:
/^\D*$/
It means, any number of not-a-digit characters. See it in action…
The alternative is to reverse your test. Just check if there's a digit present, using the trivial:
/\d/
…and if that matches, your string fails.
You're looking for a character class: ^[A-Za-z.,!##$%^&*()=+_-]+$.
The ^ and $ anchor the regex by marching the beginning and end of the string, respectively.
what about:
var re = /^[a-zA-Z!#$%]+$/;
Fell free to add any special character you need inside the character class

Categories