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 5 years ago.
Improve this question
Good morning,
If I have a string:
"#Hello(first second), one two"
And I want an array:
["#Hello(first second)", "one", "two"]
How I can do this?
Thanks,
You can use .split() with RegExp /\s(?!\w+\))/ to match space character not followed by word characters followed by closing parenthesis ")"
var str = "#Hello(first second), one two";
var res = str.split(/\s(?!\w+\))/);
console.log(res);
Alternatively you can use .match() with RegExp /#\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g to match "#" followed by one or more word characters followed by "(" followed by word characters followed by space character followed by word characters followed by closing parenthsis ")" or word characters followed by space or end of string
var str = "#Hello(first second), one two";
var res = str.match(/#\w+\(\w+\s\w+(?=\))\)|\w+(?=\s|$)/g);
console.log(res);
Related
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)
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 2 years ago.
Improve this question
const item = "Dell Model 23506A Laptop M2";
const item2 = item.replace(/\d{2,}/g, "");
The above code will remove any words with "more than 2 digits found in a row, but will only remove the numbers from the Word.
Example the end result of the above will be "Dell Model A Laptop M2", leaving the A from 23506A.
How do you write the logic, that if more than 2 digits found in a row in a Word, to remove the entire word as a result.
Example the end result of the above should be "Dell Model Laptop M2"
Thus removing 23056A entirely, because more than 2 digits we're found in a row (right after another).
This should work:
item.replace(/[^\s]*\d{2,}[^\s]*/g, "");
You may also wish to get rid of the adjacent space:
item.replace(/[^\s]*\d{2,}[^\s]*\s?/g, "");
If you don't want to remove all other strings that could possibly contain 2 digits like for example 1,50, you can assert at least a char a-zA-Z
\b(?=\w*[A-Za-z])\w*\d{2}\w*\b
Explanation
\b A word boundary
(?=\w*[A-Za-z]) Positive lookahead, assert a char a-zA-Z
\w*\d{2}\w* Match 2 digits between optional word characters
\b A word boundary
Regex demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need validate a input using regex (i think it's better) but in this input can be one or more "sentences" and be [A-Z] size 1. How can i do that?
E.g.:
A,B,D,G,J,X no repeat letters but this validate i do in code. I think regex is better 'cause validate a entire sentence instead letter by letter using a loop and split. My english is rusty, appreciate some help to improve =)
Note, Assumption is you want a single letter
If you just want to validate:
if (/([A-Z]*)?,([A-Z]*),?/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
If you are using to get extract values:
result = subject.match(/([A-Z]*)?,([A-Z]*),?/g);
Maybe this can help you ([A-Z],)+[A-Z] it will match a serie of uppercase letter followed by comma, and end with uppercase letter :
regex demo
A,B,D,G,J,X -> matches
A,B,DE,G,J,X -> not matches
A,B,D,G,J,XY -> not matches
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 6 years ago.
Improve this question
I have textbox and I want get string value. But I want users to not be able to enter the string that has number on first letter. As matter of fact I want to replace number with '' null.
for example
1test =====convert=======> test
you can simply use ^[a-zA-Z]
^ starts with a-z or A-Z
or if you want special character too then use ^\D
^\D : Matches anything other than a decimal digit
Regex Demo
you can use $text.replace(/^[^0-9]+/, '')
/^ beginning of the line
[^0-9]+ match anything other than digits at-least once
thanks # Wiktor and Tushar
here is the solution: You can check on this live regex.
https://regex101.com/r/OJfyv4/1
$re = '/\b[a-z][a-z0-9]*/';
$str = '1test';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
This works your case:
^\d+
https://regex101.com/r/daezA9/1
^ asserts position at start of the string
\d matches a digit (equal to [0-9])
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