Javascript regex match (random string in the middle) [closed] - javascript

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 want to test if a string can match a pattern in javascript
problem is there is a random string in the middle
pattern: "account/os/some_random_string/call_back"
so a string look like below will match
var mystring = "account/os/1234567/call_back"
thanks

You want a regex for starts with account/os/ and ends with /call_back, here's one:
/^account\/os\/.*\/call_back$/
.* will match any random string (including the empty string.). If you want a minimum length on the random string you change the *:
.* : 0 or more characters
.+ : 1 or more characters
.{n,} : n or more characters (replace n with an actual number)

Well, it depends. If you want every single character between account/os/ and /call_back, use this:
var randomStr = mystring.match(/account\/os\/(.*)\/call_back/)[1];
The match will return an array with 2 elements, the first one with the entire match, the 2nd one with the group (.*). If you are completely sure that you have at least one character there, replace * with +.
If you know something more specific about the text you have to collect, here are some replacements for the .(dot is matching almost everything):
[A-z] for any of A, B, .. , Y, Z, a, b, .. , y, z
[0-9] for any digit
You can mix then and go fancy, like this:
[A-Ea-e0-36-8]
So, your pattern may look like this one:
/account\/os\/([A-Ea-e0-36-8]*)\/call_back/
Your example have a number there, so you are probably looking for:
/account\/os\/([0-9]*)\/call_back/
or
/account\/os\/(\d*)\/call_back/
.. it's the same thing.
Hope that helps.
Edit: What JS answer doesn't have a jsfiddle? http://jsfiddle.net/U2Jhw/

Related

javascript regex get specific substring [closed]

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 6 months ago.
Improve this question
How could I get the specific substring between 'Y___(string i want to get)___N'?
For example:
"Y___INT_GET_ERROR_CONFIGS___N"
"INT_GET_ERROR_CONFIGS"
You can get everything inside by Y___(.*?)___N, you can use matchAll to get all instance that matches this case, and you can loop through and get the group value.
const str = `Y___INT_GET_ERROR_CONFIGS___N
INT_GET_ERROR_CONFIGS Y___INT_GET_ERROR_SOMETHING___N`
const result = str.matchAll(/Y___(.*?)___N/g);
for (match of result) {
console.log(match[1])
}
If it's just the first occurrence you wish you match, then:
'Y___(string you want to get)___N'.match(/(?<=Y___).+(?=___N)/)
Result:
"(string you want to get)"
If you want all such occurrences to be returned, then use the g flag:
`Y___(string you want to get)___N
.
.
.
Y___(second string you want)___N`.match(/(?<=Y___).+(?=___N)/g)
Result:
["(string you want to get)", "(second string you want)"]
Explanation:
(?<=Y___): A positive lookbehind stipulates that matches will be preceded by the contents of the lookbehind, namely "Y___". The contents of the lookbehind does not form part of the match result, and also does not consume characters during matching.
.+: Matches at least one instance of any character, but will match as many as possible.
(?=___N): A positive lookahead stipulates that matches will be proceeded by the contents of the lookahead, namely "___N". The contents of the lookahead does not form part of the match result, nor does it consume characters during matching.

How to remove digits from string. based on the length and remove other letters with it [closed]

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

Regex first letter not integer with jquery [closed]

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])

(\d+,\d+) add so it is imuneto white spaces [closed]

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

I need Regular Expression for a string [closed]

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 8 years ago.
Improve this question
I need a regular expression for a string that
starts with Alphabets (no number)
max Length 8
No special characters or space.
string can have number or _ except for starting character.
This would work:
/^[a-z][a-z0-9_]{0,7}$/i
For example,
/^[a-z][a-z0-9_]{0,7}$/i.test('a1234567'); // true
/^[a-z][a-z0-9_]{0,7}$/i.test('01234567'); // false
The \w shorthand is for all letters, numbers and underscores. [A-Za-z] is overkill, the /i flag will get you all letters, case insensitive.
Therefore, a super simple regex for what you need is:
/^[a-z]\w{0,7}$/i
/^[a-z]\w{0,7}$/i.test("a1234567");
> true
/^[a-z]\w{0,7}$/i.test("a12345697");
> false
/^[a-z]\w{0,7}$/i.test("01234567");
> false
Try this out:
/^[A-Za-z]{1}[a-zA-Z0-9_]{0,7}$/
Try this one:
/^[a-zA-Z][0-9a-zA-Z_]{0,7}$/
This requires an alpha start character, and optionally allows up to 7 more characters which are either alphanumeric or underscore.
EDIT: Thanks, Jesse for the correction.
And another version with lookaheads :)
if (subject.match(/^(?=[a-z]\w{0,7}$)/i)) {
// Successful match
}
Explanation :
"^" + // Assert position at the beginning of the string
"(?=" + // Assert that the regex below can be matched, starting at this position (positive lookahead)
"[a-z]" + // Match a single character in the range between “a” and “z”
"\\w" + // Match a single character that is a “word character” (letters, digits, etc.)
"{0,7}" + // Between zero and 7 times, as many times as possible, giving back as needed (greedy)
"$" + // Assert position at the end of the string (or before the line break at the end of the string, if any)
")"

Categories