javascript regexp "subword" replace - javascript

I have a phrase like
"everything is changing around me, wonderfull thing+, tthingxx"
and I want to modify every word that contains ***thing at the end of that word, or at most another character after "thing", like "+" or "h" or "x"...
something like
string = 'everything is changing around me, wonderful thing+, tthingxx'
regex = new RegExp('thing(\\+|[g-z])$','g');
string = string.replace(regex, '<b>thing$1</b>');
what I want? everything is changing around me, wonderful thing+, tthingxx
The result of my regexp? anything working... if I remove the $ all the words containing "thing" and at least another character after it are matched:
everything is changing around me, wonderful thing+, tthingxx
I tryed everything but - in first place I can't understand very well technical english - and second I did't find the answer around.
what I have to do??? thanks in advance
the solution I found was using this regular expression
/thing([+g-z]){0,1}\b/g
or with the RegExp (I need it because I have to pass a variable):
myvar = 'thing';
regex = new RegExp(myvar + "([+g-z]){0,1}\\b" , "g");
I was missing the escape \ when doing the regular expression in the second mode. But this isn't enough: the + goes out of the < b > and I don't really know why!!!
the solution that works as I want is the one by #Qtax:
/thing([+g-z])?(?!\w)/g
thank to the community!

To solve the issue with + not matching when using \b you could use (?!\w) instead of \b there, like:
thing[+g-z]?(?!\w)

Use boundary in your regex
\b\w+thing(\+|[g-z])?\b

If I understand what you want, then:
string = 'everything is changing around me, wonderful thing+, tthingxx';
string = string.replace(/thing(\b|[+g-z]$)/g, '<b>thing$1</b>');
...which results in:
every<b>thing</b> is changing around me, wonderful <b>thing</b>+, tthingxx
\b is a word boundary, so what the regular expression says is anywhere it finds "thing" followed by a word boundary or + or g-z at the end of the string, do the replacement.

Related

Match pattern except under one condition Regex

I'm trying to match a patterned with regex except when the pattern is escaped.
Test text:
This is AT\&T® is really cool Regex
You can see with my \& I'm manually escaping. And therefore, do not want the regex to match.
Regex:
const str = 'This is AT\&T® is really cool Regex'
str.replace(/\&(.*?)\;/g, '<sup>&$1;</sup>');
Expected output
This is AT&T<sup>®</sup> is really cool Regex
Hard to explain I guess but when the start of this regex looks for a & and ends with a ; however, if & is preceded with at \ like \& than do not match and look for the next \&(.*?)\;
You can use negative lookbehind
This regex works fine with the example
/(?<!\\)\&(.*?)\;/g
Edit 1
To workaround in JS you can use [^\\] that will match everything except backslash. The overall regex /[^\\]\&(.*?)\;/g It works for your example.
Since JavaScript have no support for lookbehind assertions - it is possible to add some custom substitution logic to achieve desired results. I've updated test string with examples of different kinds of html entities for test purposes:
const str = '&T;his is AT\\&T® is & really &12345; &xAB05; \\&cool; Regex'
console.log(str.replace(/&([a-z]+|[0-9]{1,5}|x[0-9a-f]{1,4});/ig, function (m0, m1, index, str) {
return (str.substr(index - 1, 1) !== '\\') ? '<sup>' + m0 + '</sup>' : m0;
}));

I need some help for a specific regex in javascript

I try to set a correct regex in my javascript code, but I'm a bit confused with this. My goal is to find any occurence of "rotate" in a string. This should be simple, but in fact I'm lost as my "rotate" can have multiple endings! Here are some examples of what I want to find with the regex:
rotate5
rotate180
rotate-1
rotate-270
The "rotate" word can be at the begining of my string or at the end, or even in the middle separated by spaces from other words. The regex will be used in a search-and-replace function.
Can someone help me please?
EDIT: What I tried so far (probably missing some of them):
/\wrotate.*/
/rotate.\w*/
/rotate.\d/
/\Srotate*/
I'm not fully understanding the regex mechanic yet.
Try this regex as a start. It will return all occurrences of a "rotate" string where a number (positive or negative) follows the "rotate".
/(rotate)([-]?[0-9]*)/g
Here is sample code
var aString = ["rotate5","rotate180","rotate-1","some text rotate-270 rotate-1 more text rotate180"];
for (var x = 0; x < 4; x++){
var match;
var regex = /(rotate)([-]?[0-9]*)/g;
while (match = regex.exec(aString[x])){
console.log(match);
}
}
In this example,
match[0] gives the whole match (e.g. rotate5)
match[1] gives the text "rotate"
match[2] gives the numerical text immediately after the word "rotate"
If there are multiple rotate stings in the string, this will return them all
If you just need to know if the 'word' is in the string so /rotate/ simply will be OK.
But if you want some matching about what coming before or after the #mseifert will be good
If you just want to replace the word rotate by another one
you can just use the string method String.replace use it like var str = "i am rotating with rotate-90"; str.repalace('rotate','turning')'
WHy your regex doesnt work ?
/\wrotate.*/
means that the string must start with a caracter [a-zA-Z0-9_] followed by rotate and another optional character
/rotate.\w*/
meanse rotate must be followed by a character and others n optional character
...............
Using your description:
The "rotate" word can be at the beginning of my string or at the end, or even in the middle separated by spaces from other words. The regex will be used in a search-and-replace function.
This regex should do the work:
const regex = /(^rotate|rotate$|\ {1}rotate\ {1})/gm;
You can learn more about regular expressions with these sites:
http://www.regular-expressions.info
regex101.com and btw here is an example using your requirements.

Regex trying to match characters before and after symbol

I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+) but, the problem is that my result is: -closed instead of closed.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex http://jsfiddle.net/trDFh/1/
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring is a solution as well: http://jsfiddle.net/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if in a more complex if syntax, and the chosen solutions seems to be the most fitted for now.
Use exec():
var result=/([^-]+)-([^-]+)/.exec(string);
result is an array, with result[1] being the first captured string and result[2] being the second captured string.
Live demo: http://jsfiddle.net/Pqntk/
I think you'll have to match that. You can use grouping to get what you need, though.
var str = 'budgets-closed';
var matches = str.match( /([a-z]+)-([a-z]+)/ );
var before = matches[1];
var after = matches[2];
For that specific string, you could also use
var str = 'budgets-closed';
var before = str.match( /^\b[a-z]+/ )[0];
var after = str.match( /\b[a-z]+$/ )[0];
I'm sure there are better ways, but the above methods do work.
If the symbol is specifically -, then this should work:
\b([^-]+)-([^-]+)\b
You match a boundry, any "not -" characters, a - and then more "not -" characters until the next word boundry.
Also, there is no need to escape a hyphen, it only holds special properties when between two other characters inside a character class.
edit: And here is a jsfiddle that demonstrates it does work.

Replace string everywhere except if its within quotes

I would like to replace all :) by :D, except if they are within quotes "
Example 1:
Hey man :D, how're you? :) My friend told me "this can't be true :)"
becomes
Hey man :D, how're you? :D My friend told me "this can't be true :)"
As you see, :) is not replaced if it's enclosed by ". If this condition wouldn't be there, it would be quite simple, right? I am using Javascript (jQuery) for all this.
If this is not plainly possible with a regex, what would be an alternate suggestion?
Assuming no double quote is unbalanced, this is the regex that should work for you:
:\)(?=(?:(?:[^"]*"){2})*[^"]*$)
Explanation: This regex is using a positive lookahead that basically is matching 0 or more occurrences of a pair of some text until a double quote is found i.e. ([^"]*"){2} on the right hand side (RHS) of every match of :).
Which in simple term means replace a :) only if it is outside double quotes since all the matches inside double quotes will have odd number of [^"]*" matches on RHS.
Live Demo: 1. http://www.rubular.com/r/3aixZy5bYR
Live Demo: 2. http://ideone.com/C679NW
function parseSmiley(str){
return str.replace(/(?!"):\)(?!")/g, ":D");
}
console.log(parseSmiley('Hey man :D, how\'re you? :) My friend told me "this can\'t be true :)"');
And the fiddle with an input so you can test.
Basically, we just replace all instances of :) not encased in " " with :D
If you need variables to control of this - then you could try
function doReplace(find, repl, str, fullWordsOnly, ignoreSQuotes, ignoreDQuotes, caseSensitive) {
if (caseSensitive == undefined) caseSensitive = false;
var flags = 'gm';
if (!caseSensitive) flags += 'i';
var control = escapeRegExp(find);
if (ignoreDQuotes) control = '(?!\\B"[^"]*)' + control + '(?![^"]*"\\B)';
if (ignoreSQuotes) control = '(?!\\B\'[^\']*)' + control + '(?![^\']*\'\\B)';
if (fullWordsOnly) control = '\\b' + control + '\\b';
return str.replace(new RegExp(control, flags), repl);
}
Note, this will probably have problems with single quotes when using abbreviated words such as how're and can't, so I've also included a hack to fix against that:
// Fix any abbreviated words ('re or 't)
str=str.replace(/'re\/b/gi, '&apos;re');
str=str.replace(/'t\b/gi, '&apos;t');
See http://jsfiddle.net/Abeeee/ct7vayz5/4/ for a working demo

Javascript regex expression to replace multiple strings?

I've a string done like this: "http://something.org/dom/My_happy_dog_%28is%29cool!"
How can I remove all the initial domain, the multiple underscore and the percentage stuff?
For now I'm just doing some multiple replace, like
str = str.replace("http://something.org/dom/","");
str = str.replace("_%28"," ");
and go on, but it's really ugly.. any help?
Thanks!
EDIT:
the exact input would be "My happy dog is cool!" so I would like to get rid of the initial address and remove the underscores and percentage and put the spaces in the right place!
The problem is that trying to put a regex on Chrome "something goes wrong". Is it a problem of Chrome or my regex?
I'd suggest:
var str = "http://something.org/dom/My_happy_dog_%28is%29cool!";
str.substring(str.lastIndexOf('/')+1).replace(/(_)|(%\d{2,})/g,' ');
JS Fiddle demo.
The reason I took this approach is that RegEx is fairly expensive, and is often tricky to fine tune to the point where edge-cases become less troublesome; so I opted to use simple string manipulation to reduce the RegEx work.
Effectively the above creates a substring of the given str variable, from the index point of the lastIndexOf('/') (which does exactly what you'd expect) and adding 1 to that so the substring is from the point after the / not before it.
The regex: (_) matches the underscores, the | just serves as an or operator and the (%\d{2,}) serves to match digit characters that occur twice in succession and follow a % sign.
The parentheses surrounding each part of the regex around the |, serve to identify matching groups, which are used to identify what parts should be replaced by the ' ' (single-space) string in the second of the arguments passed to replace().
References:
lastIndexOf().
replace().
substring().
You can use unescape to decode the percentages:
str = unescape("http://something.org/dom/My_happy_dog_%28is%29cool!")
str = str.replace("http://something.org/dom/","");
Maybe you could use a regular expression to pull out what you need, rather than getting rid of what you don't want. What is it you are trying to keep?
You can also chain them together as in:
str.replace("http://something.org/dom/", "").replace("something else", "");
You haven't defined the problem very exactly. To get rid of all stretches of characters ending in %<digit><digit> you'd say
var re = /.*%\d\d/g;
var str = str.replace(re, "");
ok, if you want to replace all that stuff I think that you would need something like this:
/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g
test
var string = "http://something.org/dom/My_happy_dog_%28is%29cool!";
string = string.replace(/(http:\/\/.*\.[a-z]{3}\/.*\/)|(\%[a-z0-9][a-z0-9])|_/g,"");

Categories