Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
How to use regex to replace all the occurrences of space ( . ) '- with underscores.
I used chaining of replaceAll. Is there a better approach?
const str = "Certificate of Naturalization (From N-550 or N-570) or Certificate of U.S Citizenship (From N-560 or N-561)";
console.log(((str).toUpperCase()).replaceAll(' ', '_').replaceAll('\'', '_').replaceAll('(', '').replaceAll(')', '').replaceAll('-', '_').replaceAll('.', '_'));
You can put the characters you want to replace inside [] and you need to escape some special characters with \
there are 12 characters with special meanings: the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {, These special characters are often called “metacharacters”. Most of them are errors when used alone.
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.
const str = `Certificate of Naturalization (From N-550 or N-570) or Certificate of U.S Citizenship (From N-560 or N-561)`;
const result = str.replace(/[-'\s\(\)\.]/g, '_')
console.log(result)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two words within sentence.
EX: big BUTTON
here I need to check second word is in uppercase using regex expression.
Square brackets [] are your friend. They allow you to specify characters that will match. To match the first work, you need to check for any letter. This can be done with [a-zA-Z]. This will match any letter between a and z, as well as A and Z. for the second word, you only want to match uppercase, so only use [A-Z]. To get 1 or more matches, put a + after the closing bracket.
Putting this all together, with a space in between the words, you get [a-zA-Z]+ [A-Z]+.
The carat ^ is used to signify the start of the string, and the dollar sign $ is used to signify the end of the string. Your question somewhat vague, so here are a couple scenarios:
Each sentence is only two words with a space in between them: ^[a-zA-Z]+ [A-Z]+$
Each sentence has at least two words and may or may not end in a period: ^[a-zA-Z]+ [A-Z]+( |\.?$)
In the second example the parenthesis with a pipe (|) is used as an OR statement. The period is escaped since it is a special character (matches any single character). The question mark denotes 0 or 1 of the preceding character, which is a period. So ( |\.?$) will match a space or a sentence that ends with or without a period.
Here is a good site that has information on Regexes: http://www.regular-expressions.info/
This regexp looks for any sequence, starting at the beginning of the string (^), of alphanumeric characters (\w)--that's the first word--then a space, followed by a sequence of upper-case letters ([A-Z]+)--the second word--followed by either a space or the end of the string ($).
/^\w+ [A-Z]+( |$)/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How in javascript to make regex to recognize and extract integer numbers for coordinates which have format like
( number1, number2 )
between ( and number1 and , and number2 and ) can be arbitrary number of whitespaces (user are going to enter coordinates so I don't want to force strict format without whitespaces)
(\d+,\d+)
what to add to this so it works ?
There are a few choices, pending your actual input.
To match all "whitespace characters" (space, tab, carriage return, newline and form feed), you can use the \s shorthand approach. If you want a number, in this case \d+, to be surrounded by "0 or more" of these, you would use:
\s*\d+\s*
In your full pattern:
\( # opening parentheses
\s*\d+\s*, # first number followed by a comma
\s*\d+\s* # second number
\) # closing parentheses
Note: The parentheses are escaped here as they're special characters in a regular expression pattern.
Now, if you don't want to match "all whitespace" and were only interested in plain spaces, for example, you could use a matching character set of [ ] (i.e. a space between two brackets). In the pattern from above:
\(
[ ]*\d+[ ]*,
[ ]*\d+[ ]*
\)
Not really sure how you want to use the matches, I'm assuming you want the numbers returned individually so in that case, you can use the following:
var str = '(1, 2)';
var matches = str.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);
if (matches) {
var firstNumber = matches[1];
var secondNumber = matches[2];
// do stuffs
}
Note: In the pattern I used here, I've wrapped the \d+s in parentheses; this will "capture" those values in to groups which are then accessible by their "group index". So, the first (\d+) will be available in matches[1] and the second will be available in matches[2].
Try this regex: \(\s*\d+\s*,\s*\d+\s*\).
Fiddle
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
a string like this: "01A123,02A13334,03A99313,01BA9424,……"
substring's regex is: /\d{2}[A-Z]{1,2}\d*/
can we write a regex to match this string without split it?
To validate the entire line is of this form, something like this
# /^\d{2}[A-Z]{1,2}\d*(?:,\d{2}[A-Z]{1,2}\d*)*$/
^ # Beginning of string
\d{2} [A-Z]{1,2} \d* # 2 digits, 1-2 A-Z, optional 0-many digits
(?: # Cluster group start (non-capture group)
, # comma ','
\d{2} [A-Z]{1,2} \d* # 2 digits, 1-2 A-Z, optional 0-many digits
)* # Cluster group end, optional 0-many times
$ # End of string
You can use:
"01A123,02A13334,03A99313,01BA9424".match(/\d{2}[A-Z]{1,2}\d*/g);
["01A123", "02A13334", "03A99313", "01BA9424"]
Yes, You can Your regex \d{2}[A-Z]{1,2}\d* can be represented as,
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a following requirement to only allow capital letters and , in a javascript form . I am unsure on how to check for special characters and script tags . I have written the following code . I do not want to allow characters such as $,%,& etc .
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('^[a-z]');
var numbers = new RegExp('^[0-9]');
if($(this).val().match(upperCase) && $(this).val().match(lowerCase) && $(this).val().match(numbers))
{
$("#passwordErrorMsg").html("OK")
}
Based on what you've given us, this may suit the bill. It will determine if any characters are not in character classes a-z, A-Z or 0-9 but note this will also treat é or similar characters as rejected symbols.
So if the value is 'test_' or 'test a' it will fail but it will pass for 'testa'. If you want it to accept spaces change the regex to /[^a-zA-Z0-9 ]/.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
This may be helpful.
javascript regexp remove all special characters
if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those
$(this).val().replace(/[^\w\s\][^,]/gi, '')
This link may be helpful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
It has a lot of information on JS Regexps.
For a dollar sign ($), the regexp syntax would be: \$. You need to escape the special character so it is read as a literal. The syntax would be the same for the other special characters, I believe.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my website , i need a REGEXP in java script for validation TITLE which can take alphabates, digits and Special char set [, / ( ) & - : . space], but if any user enter only single and double spaces or single or double .. like [..] in title or double digit [1 2] then it's should not allowed, atlest one aplhabate is required. please help
You can use this pattern:
^[-a-z0-9,/()&:. ]*[a-z][-a-z0-9,/()&:. ]*$
This will match any number of your special characters followed by a Latin letter, followed by number of your special characters. It's effectively equivalent to [-a-z0-9,/()&:. ]+ except it requires at least one [a-z] somewhere in the string.
Of course, you need to escape the \ when written as a regex literal in javascript, and you probably want to use the i flag for case-insensitive matching:
var pattern = /^[-a-z0-9,\/()&:. ]*[a-z][-a-z0-9,\/()&:. ]*$/i