No white space at beginning + allow space in the middle [duplicate] - javascript

This question already has answers here:
Regular expression: match start or whitespace
(8 answers)
Closed 1 year ago.
I have this regex for detecting #xxx
/(?:#)(.*[a-zA-Z0-9]*)/
it matches even when the #xxx is not separated from another string from the left (when it's typed in the middle of an input line).
xxx#xxx will match too so i added \s to require a space in the begining .Now it's
/\s(?:#)(.*[a-zA-Z0-9]*)/
But the problem is there isn't a match when the #xxx is typed in the begining of a line (the white space is still required) and i need it match in that case.
I tried to get inspired by https://stackoverflow.com/a/19973707/170592 so i added ^[^-\s] in the begining of the regex to make it
/^[^-\s](?:#)(.*[a-zA-Z0-9]*)/
But it didn't work neither.

I think that what you are looking for it is /\S+/ which means that check for any non-whitespace and I don't think you need the ^ at the beginning.
[-\S+](?:#)(.*[a-zA-Z0-9]*)

Related

Regex to match middle string inside optional whitespace [duplicate]

This question already has answers here:
How to remove leading and trailing white spaces from a given html string?
(7 answers)
Closed 5 years ago.
I'm trying to create a regular expression that will match any given string (text or whitespace) inside of arbitrary, optional whitespace. The string itself could have whitespace in it;
I'm just trying to cut white space of the beginning and end, if it exists.
Example strings:
one
t
three
four
five
Expected output:
one
t
three
four
five
I've been testing on regextester.com but have so far haven't been able to get it quite right.
[^\s][\w\W]*[^\s] will match cases 1, 3, 4, and 5, but it fails for single-character strings.
[^\s]*[\w\W]*[^\s] gets 1, 2, and 4, but it includes the leading whitespace from 3 and 5.
Is there a regular expression can handle this task? I'd also settle for using option 2 above and then trimming off the leading whitespace afterwards, but not sure how to do that.
You don't need regex to strip whitespace. In python just use the .strip method of any text object. I am sure other languages have an equally convenient tool.
In java you can use the .trim() method on any String. This will remove leading and trailing whitespace
" spaces at front and end. ".trim() -> "spaces at front and end"

RegEx: non-consecutive special characters only allowed in the middle [duplicate]

This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference

Using Javascript RegEx to find the last whitespace before inserting linebreak except for some punctuation [duplicate]

This question already has an answer here:
Split long string into text chunks with jQuery
(1 answer)
Closed 7 years ago.
Here's the statement I have:
text.replace(/(.{35})/g, "$1\n");
It works, it inserts a new line every 35 characters. However, I don't want a word to be cut in half. How would I find the last whitespace BEFORE that 35th character? Is it possible to do with RegEx? The block it executes on should insert up to 6 linebreaks because the overall character limit is 210 characters.
This is the current output:
This is an example of current output:
this is some text that has been for
matted by that statement.
This is what I want:
This is an example of current output:
this is some text that has been
formatted by that statement.
It is being executed on a text field.
You can use the following regex:
text.replace(/.{0,35}\b/g, "$&\n");
See demo
Capturing groups are redundant here since we can access the matched text with $&. \b ensures whole word match, that we match at the word boundary. {0,35} is a greedy limiting quantifier (that is, it tries to match as many characters as it can), but matching will end before the 35th character if there is a word boundary earlier and 35th character is not at the boundary position.
EDIT:
So as not to insert the linebreak at the end of the string, and also keep a punctuation symbol in the character class on the current line, use
.{1,35}(?:[.,:;–—-]|\b)
See another demo

Regex pattern to match this string [duplicate]

This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group

regex remove white space after text [duplicate]

This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 4 years ago.
From this regex,
text.replace(/^\s+|\s+$/g,"").replace(/ +/g,' ')
how do I remove the regex just for trailing white space?
I am new to regex and did some research but I'm not able to understand the pattern.
/^\s+|\s+$/g means
^ // match the beginning of the string
\s+ // match one or more whitespace characters
| // OR if the previous expression does not match (i.e. alternation)
\s+ // match one or more whitespace characters
$ // match the end of the string
The g modifier indicates to repeat the matching until no match is found anymore.
So if you want to remove the part the matches whitespace characters at the end of the string, remove the |\s+$ part (and the g flag since ^\s+ can only match at one position anyway - at the beginning of the string).
Useful resources to learn regular expressions:
http://www.regular-expressions.info/
Regex in JavaScript (since this seems to be JavaScript).

Categories

Resources