Using Javascript, I am trying to split a text using a regular expression that contains only one letter and a point; for example 'A.' or 'Q.'
I am using: array[0].split(/[A-Z]+./);
But it is returning true if there are multiple characters like 'AA.' or 'ZZ.'
What is the regex for one character only? I also have access to jQuery.
Thank you!
This should work: array[0].split(/([A-Z]{1}\.)/);
Explanation:
[A-Z]{1} - this looks for any single letter
\. - this looks for a . note that you'll need the backslash as a . in a regex means any character.
These are surrounded by brackets so that you are looking for the sequence:
one letter followed by a .
You'll need to remove the + as it means one or more. if you want to find several occorances then you'll need the global flag (g at the end): array[0].split(/([A-Z]{1}\.)/g);
Limit to A. and not AA.
If you want to make sure its only one letter follow by a . and not match on the end of AA. then you can use the word boundary like so: array[0].split(/\b([A-Z]{1}\.)/);
This site is amazing for helping with regex: https://regex101.com/
You don't want to use +, as it means "one or more". Just remove it. Also you have to escape the . otherwise it means "any character".
If you want to match only a capital letter and a point, this should do the trick : /[A-Z]\./g if you want the regex to be case insensitive, simply add the i flag : /[A-Z]\./gi. Remove the g flag if you only want to match the first occurrence.
You can try this
one
/\s[A-Z]\.\s/g
Related
I want to match everything beginning from second ., including .
Regexp: /(?<=\d\.\d+)\..*/g. Playground regex101
It does not work for strings 1232..233232.
Update
as #WiktorStribiżew points out the regex don't test for 1212.2e1.121212 os this might be a better solution.
/(?<=^[^.]*\.[^.]*)\..*/ since it will also test for this
old answer.
You can do this regex101, this will begin including from the second . including it.
Regexp: /(?<=\d?\.\d*)\..*/g
You need to use * (include 0 to x elements of this character) instead of + (include 1 to x of this character)
I have added a ? after your first \d to handle the case if it starts with a . and not a digit.
When reading your question literally.
I want to match everything beginning from second ., including .
This would do the trick:
[.][^.]*([.].*)
Leaving the resulting answer in group 1. Keep in mind that [^.] also matches newline characters, if you don't want this add \n to the character negation class.
how to write regular expression allow name with one space and special Alphabets?
I tried with this [a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)* but not working for me,
example string Björk Guðmundsdóttir
You may try something along these lines:
^(?!.*[ ].*[ ])[ A-Za-zÀ-ÖØ-öø-ÿ]+$
The first negative lookahead asserts that we do not find two spaces in the name. This implies that at most one space is present (or no spaces at all). Then, we match any number of alphabets, with most accented letters included. Spaces can also be matched, but the lookahead would already ensure that at most one space can be present.
Demo
Use this one:
[a-zA-Z\u00C0-\u00ff]*[ ]{1}[a-zA-Z\u00C0-\u00ff]*
Answer from other question
I am trying to capture varients of a word using Microsft Word find and replace function. Here is a searchable snippet:
There are going to be 3 instances of the word successful for the purpose of Regex matching. Here is the second sucesfull and here is another succesfull , both spelt incorrectly.
This is my Regex expression used in Find and Replace with "Use Wildcards" selected (I have also tried this with replacing the braces with brackets with no joy)
<([Ss]uc[1,]es[1,]ful[1,])>
[Ss]uc{1,}es{1,}ful{1,}
Replace the [ ] with { } and it should work fine. The curly braces specify how many times you want a character to repeat. Square brackets are used to specify the acceptable characters.
So the current regular expression will match the following.
succcccesssfulll
sucesful
successful
Successsssfull
and so on.
I think this is cleaner and easier to type.
[Ss]uc+es+ful+
"+" counts for one or more occurrence of a character.
The search string you want would be:
<[sS]uc#es#ful#>
This searches for a word (the < and > symbols) starting with either s or S and including one or more (the # symbol) of c, s, and l.
I am trying to use regexp to match some specific key words.
For those codes as below, I'd like to only match those IFs at first and second line, which have no prefix and postfix. The regexp I am using now is \b(IF|ELSE)\b, and it will give me all the IFs back.
IF A > B THEN STOP
IF B < C THEN STOP
LOL.IF
IF.LOL
IF.ELSE
Thanks for any help in advance.
And I am using http://regexr.com/ for test.
Need to work with JS.
I'm guessing this is what you're looking for, assuming you've added the m flag for multiline:
(?:^|\s)(IF|ELSE)(?:$|\s)
It's comprised of three groups:
(?:^|\s) - Matches either the beginning of the line, or a single space character
(IF|ELSE) - Matches one of your keywords
(?:$|\s) - Matches either the end of the line, or a single space character.
Regexr
you can do it with lookaround (lookahead + lookbehind). this is what you really want as it explicitly matches what you are searching. you don't want to check for other characters like string start or whitespaces around the match but exactly match "IF or ELSE not surrounded by dots"
/(?<!\.)(IF|ELSE)(?!\.)/g
explanation:
use the g-flag to find all occurrences
(?<!X)Y is a negative lookbehind which matches a Y not preceeded by an X
Y(?!X) is a negative lookahead which matches a Y not followed by an X
working example: https://regex101.com/r/oS2dZ6/1
PS: if you don't have to write regex for JS better use a tool which supports the posix standard like regex101.com
I need to check if the number is next to a letter, and if so, add an underscore.
For example:
Grapes23 --> Grapes_23
I've tried for quite a while, but I'm new to regular expressions. I tried this but it doesn't work:
str=str.replace(/([A-z]+)([0-9])/i, '_'); //if number next to letter, add underscore
I'd appreciate any help, thank you!
Look for a letter followed by a number:
str = str.replace(/([a-z])(?=[0-9])/ig, '$1_');
http://regexr.com?31qsr
How this regular expression works:
([a-z]) is any lowercase letter, wrapping it in parens makes it a
"matching group"
(?=[0-9]) is a "lookahead". it basically means "followed by [0-9] (any digit)"
i means ignore case (otherwise we would have to use [a-zA-Z])
g means global, or replace every match it finds (default only replaces the first one)
$1 means "first matching group", or the letter that was matched by the first bullet above.
Run str.replace(/([a-zA-Z])(\d)/g,'$1_$2') on your string. This will look for any letter followed by a number, capture both the letter and number (note the parentheses) and then replace them with an underscore between the two. $1 and $2 are callbacks to the captured letter and number found in the regular expression match.
The easiest approach is:
string.replace(/(\D)(\d)/,'$1_$2')
JS Fiddle.
Note that this will only replace the first instance, if you wish to replace all instances, then I'd suggest the above, but with the g (global) flag:
string.replace(/(\D)(\d)/g,'$1_$2')
JS Fiddle.