I have this string:
++some+text+to+replace+stay,++some+text+to+replace+stay,++some+text+to+replace+stay
I want to replace everything between the + characters where no , is included.
So the result should look like:
_stay,_stay,_stay
("_" is the replaced space)
I've already played around a bit and got this working: (\+[^\,]+)(?=[^,](?:,|$))
Debuggex Demo
This is the closest I can get to the right result with my regex knowledge. Could you please help me out and explain your solution?
You started out right, but complicated everything. Simply:
\+[^,]+\+
A plus sign (\+), followed by anything that isn't a comma ([^,]+), followed by a plus sign (\+).
s.replace(/\+[^,]+(.{4})(?=,|$)/g, '_$1') // where `s` is the string
Related
I have a use case that requires a plain-text file to have lines to consist of at most 38 characters, and 'pages' to consist of at most 28 lines. To enforce this, I'm using regular expressions. I was able to enforce the line-length without any problems, but the page-length is proving to be much trickier.
After several iterations, I came to the following as a regular expression that I feel should work, but it isn't.
let expression = /(([^\f]*)(\r\n)){29,}\f/;
It simply results in no matches.
If anyone could provide some feedback, I'd greatly appreciate it! - Jacob
Edit 1 - removed code block around second expression, it was probably making my question confusing.
Edit 2 - removed following text, it's not pertinent:
As a comparison, the following expression results in a single match, the entire document. I'm assuming it's matching all lines up until the final
let expression = /(.*(\r\n)){29,}
Edit 3 - So after some thinking, I realized that my issue is due to the initial section of the regex that matches any characters before a newline is including newlines. Therefore, I believe I need to match any characters before a newline EXCEPT (\f\r\n). However, I'm now having trouble implementing this. I tried the following:
let expression = /([^\f^\r^\n]*(\r\n)){29,}\f/;
But it's also not matching. I'm assuming that my negations are wrong...
Edit 4 - I have the following regex that matches each line: let expression = /([^\f\r\n]{0,}(\r\n))/;
This is pretty close to what I want. All I need now is to match any instances of 29 or more lines followed by \f
Thanks for all the help to those who commented, a friend ended up helping me get the final regex
let expression = /([^\f\r\n]*?\r??\n){29,}?\f/;
Edit:
As you clarified more your problem, and provided your updated regex:
/([^\f^\r^\n]*(\r\n)){29,}\f/;
Your negations are not right here, use [^\f\r\n] instead of [^\f^\r^\n]. This will negate all of \f, \r, and \n.
So, your regex becomes:
/([^\f\r\n]*(\r\n)){29,}\f/;
This will match 29 or more lines of characters (that can be anything but \f, \r or \n), the whole thing followed by a single \f.
Original answer:
Your current regular expression:
let expression = /(([^\f]*)(\r\n)){29,}\f/;
Matches strings that consist of 29 or more lines (separated by \r\n), the whole thing followed by one single \f.
As far as I understood, you want each of your lines to end with \f. Did you mean to include the \f inside?
let expression = /(([^\f]*)(\r\n\f)){29,}/;
I had the following regex to match opening quotation marks:
/'(?=\b)/g
But then I realized that it also captures stuff like don't and it's.
So I added another rule:
/'(?=\b[^a-zA-Z])/g
Capture an opening quotation mark not followed by a letter (t and s in this case). But now none of the quotation marks are being highlighted.
Did I modify the regex in a wrong way?
EDIT:
Oh, I realized my dumb mistake. Anyway, here's what I want to do:
"two 'three'"
"four don't it's "
I want to match the opening ' in three but not the 's in don't and it's
You can try this regex to match quotes but skip those cases like don't it's etc:
/(?:^|[^a-zA-Z])'|'(?![a-zA-Z])/gm
RegEx Demo
You can use the following regex to only match opening '
/(?!\b)'/g
See the demo on your testcases
You have your look-around in the wrong place. You want to find instances where a quote is preceded by nothing, and proceeded by characters. This would probably work
/\b'\B/
Referring to your last EDIT, this Regex /[\.\,\;\?\!\s]+'/g is what you are looking for, it matches quotation in sentences like 'three' and skips what you need (sentences like don't and it's).
Here's a DEMO.
I just wanted to ask for an example of a string that would match this regular expression for JS:
/\/[a-z]{2,6}\/(\([0-9]+\)?$/
But this bit confuses me: /(\([0-9]+\)?$/
If I could get an example of a string using this regular expression and a brief explanation, that would be enough to clear it up for me.
Thanks!
EDITED: Sorry for the trouble, I missed a parentheses, however I just want to clear up, a string that would match would be such as:
/ab/(12345) or /abcdef/(1) etc right?
Here is a visual representation of the #Paul Roub explanation:
EDITED with last pattern
\/[a-z]{2,6}\/\([0-9]+\)$
Debuggex Demo
This example is OK : /abcdef/(12345)
\/
A forward slash (escaped), followed by
[a-z]{2,6}
between two and six lowercase English letters, followed by
\/
another slash
([0-9]+)?
the inside part - one or more digits. ? means "zero or one", so we're looking for a string of digits, or nothing. The parentheses would let this number be captured as a group for later processing
$
and the end of the string.
Things that would match:
/ab/0
/ab/
/acdefg/12345
things that would not match
/a/0
/abcdefgh/12345
/ab/0x
That regexp is malformed.
Opening bracket is escaped, but closing one not
Opening paren is not escaped but closing one is
I think the intended regexp was:
/\/[a-z]{2,6}\/([0-9]+)?$/
This would match:
/ab/1
/ab/
/abc/123
Wouldn't match:
/a/1
ab/1
/abcdefg/123
Cheers.
I have a regex, and a string that includes some matches for this regex. My regex handle all this matches like it is only one big match (of course I don't want such behaviour), let me show you an example:
My test string (sorry for scribble, but this doesn't matter):
sdfsd -dsf- sdfsdfssdfsfdsfsd -sdfsdf-
my regex in js code:
view.replace(/(\-(.+)\-)/g, '<span style="background-color:yellow">$1</span>');
my result:
sdfsd<span style="background-color:yellow">-dsf- sdfsdfssdfsfdsfsd -sdfsdf-</span>
As you can see each of this strings in the "-" must be enclosed in span, but there is only one span. How I can fix this? (honestly I don't want change my (.+) regex part, which I think might be a problem, but if there is no other way to do this, let me know).
In other words, result must be:
sdfsd<span style="background-color:yellow">-dsf-</span> sdfsdfssdfsfdsfsd <span style="background-color:yellow">-sdfsdf-</span>
Feel free to ask me in the comments, and thanks for your help.
honestly I don't want change my (.+) regex part, which I think might be a problem
Why not, it is actually the source of the problem, you can try the following regex which would work:
/(\-([^-]+)\-)/g
and if you think that dashes - can appear between - and - themselves then you can use the less efficient:
/(\-(.+?)\-)/g
+? causes a lazy match, or in other words after matching the initial -, then .+? matches a single character then it moves control to the following - which tries to match a dash, if it couldn't then .+? reads (consumes) another character from the input and so on until the following - is able to match.
You can try:
view.replace(/-([^-]+)-/g, '<span style="background-color:yellow">$1</span>');
I have a string like this: ----------- 243f,33f----
Now I want to remove all the - chars except the first -, the , and the numbers. The result should be -243,33. How would I do this?
Your question still isn't very clear, but this yields the output you want:
'----------- 243f,33f----'.replace(/(?!^-)[^\d,]+/g, '')
The regex matches one or more of any character except digits or commas, after making sure the first character isn't a hyphen at the beginning of the string.
EDIT: To those who came up with regexes using (?<!^) and then withdrew them because JavaScript doesn't support lookbehinds, that wasn't necessary. I know there were other problems with those answers, but for the purpose of matching "not the beginning of the string", (?!^) works just fine.
'----------- 243f,33f----'.replace(/[^-0123456789,]/g, '').replace(/^-/, 'x').replace(/-/g, '').replace(/x/, '-')
output: '-243,33'
replace the substring from second character ala
string.charAt(0) + string.substring(1).replace("-","");
but i don't know if the syntax is correct
EDIT:
oh if you want to remove the f's too you can remove any nondigit and non-comma:
string.charAt(0) + string.substring(1).replace("[^0-9,]","");
Try this one:
(?:(^-)|[^\d,\r\n])
and replace with
$1
See it here on Regexr
Its a bit more difficult since Javascript does not support look behinds. So if there is a - at the start of the line it will be replaced by itself, for the other matches $1 is empty so replaced with nothing.