I'm trying to work out what regular expression I would need to take a value and replace spaces with a dash (in Javascript)?
So say if I had North America, it would return me North-America ?
Can I do something like var foo = bar.replace(' ', '-') ?
It's better to use:
var string = "find this and find that".replace(/find/g, "found");
to replace all occurrences.
The best source of information for regular expressions in various languages that I've found is Regular-Expressions.info (and I linked directly to the Javascript section there).
As for your particular question, yes, you can do something like that. Did you try it?
var before = 'North America';
var after = before.replace(/ +/g, '-')
alert('"' + before + '" becomes "' + after + '"');
Use the site I showed you to analyze the regex above. Note how it replaces one or more spaces with a single hyphen, as you requested.
For the most regular expressions, you can do it by testing with the regular expression tester.
Related
I am trying to create a regular expression for the string filtering. I want to get the symbol "#" and anything that is written after that and before a space.
Can someone help me with this?
For example:
hi I am #vaibhav .
The expected result this regular expression should give is vaibhav.
I made this:
/#[a-z]*/
However, I am not sure if this will confirm to the above mentioned criteria.
To get a substring from the # up to the first space after it, use
#\S+
See demo
The \S means a non-whitespace character.
If you do not need #, use a capturing group:
#(\S+)
The value you need will be in Group 1. See another demo.
If you are using JavaScript:
var re = /#(\S+)/g;
var str = 'hi I am #vaibhav . hi, and I am #strib .';
var m;
while ((m = re.exec(str)) !== null) {
document.write("The value is: <b>" + m[1] + "</b><br/>");
}
The simplest solution is to use a negated set.
Search characters that are not '#'
Read in the '#'
Now capture characters that are not ' '
If you're trying to match and capture you can accomplish that like this:
[^#]*#([^ ]*).*
[Live Example]
If you only want to search then you don't need to match the whole string and you can just extract the actual match section:
#([^ ]*)
[Live Example]
The most complicated situation is where you need to deal with an escaped '#'. Here's an example of a match using that:
(?:[^\\#]|\\.)*#([^ ]*).*
[Live Example]
You can do that with lookarounds.
Edited version:
(?<=#)\w+
Demo on regex101
I am trying to fix this since yesterday and believe I am missing something very simple.
I wrote a regex to match ANY ONE of the three IP address format:
Pattern to match : X.X.X.X OR X.X.X.X/X.X.X.X OR X.X.X.X-X.X.X.X
Regex:
/^(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\/([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\-([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))$/
Problem:
The regex matches the above 3 formats but the problem is with the alternation symbol - the behaviour is like the regex stops once the match is found.
Example: 1.1.1.1/1.1.1.1 - Once this match is found it does not check after that.
i.e: 1.1.1.1/1.1.1.1 - Valid
But 1.1.1.1/1.1.1.1(...anything after this is also recognized as valid which should not be the case...)
Question:
How do I make it to match only one of the 3 alternatives as it is. I tried a bit with word boundaries (\b) as well, but I am not sure if that is what is needed.
Any help appreciated.
Try this regex:
(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)|(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[\/-](?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)
You can test it using the following link for verification.
https://regex101.com/r/fC6uS3/1
The problem is that alternation has the lowest precedence of all the regex constructs. Your regex matches either:
^X.X.X.X/X.X.X.X // anchored at start only
or
X.X.X.X-X.X.X.X // not anchored
or
X.X.X.X$ // anchored at end only
You can fix it by adding another set of parentheses around everything but the anchors:
^(your regex)$
Im not sure if you want to match more than one, but if that is the case then remove "^" from the beginning and "$" from the end and also make it globally search like this:
/(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\/([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\-([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))/g
Hope this helps
You can probably shorten your regex to:
^(((([0-2][0-5][0-6])|(\d{1,2}))\.){3}((([0-2][0-5][0-6])|(\d{1,2}))))([/-](((([0-2][0-5][0-6])|(\d{1,2}))\.){3}(([0-2][0-5][0-6])|(\d{1,2}))))*$
If you are running it with javascript you would surround with /.../
var pattern = new RegExp(/^(((([0-2][0-5][0-6])|(\d{1,2}))\.){3}((([0-2][0-5][0-6])|(\d{1,2}))))([/-](((([0-2][0-5][0-6])|(\d{1,2}))\.){3}(([0-2][0-5][0-6])|(\d{1,2}))))*$/);
var testCases = {};
//should work
testCases['testCaseA'] = '1.2.3.4';
testCases['testCaseB'] = '1.2.3.4/1.256.3.4';
testCases['testCaseC'] = '1.2.3.4-1.2.3.4';
//should not work
testCases['testCaseD'] = '1.257.3.4';
testCases['testCaseE'] = '1.2.3.4/1.2.3.356';
testCases['testCaseF'] = '1.2.3.4-1.2.3.4I';
var results = '<table><tr><th>Cases</th><th>Inputs</th><th>Outputs</th></tr>';
$.each(testCases, function(k, v) {
results += '<tr><td>' + k + ' </td><td>' + v + ' </td><td>' + pattern.test(v) + '</td>';
});
document.write(results + '</table>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I have a string as follows
var company = "Microst+Apple+Google";
And I want replace all the + signs with %2B
But when I use this code. It returns 0
var company = company.replace(/+/g, "%2B");
I think JavaScript thinks that + is an arithmetic operation. Is there a special symbol to be used? or can user a variable except directly using the + sign?
If so please mention. Any Idea how to do this ?
You need escape it :
var company = company.replace(/\+/g, "%2B");
It is because + is special symbol used to indicate that preceding character should match 1 or more times.
You can read more about regular expressions syntax here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
No, JavaScript doesn't think it's an arithmetic operation but + is a quantifier in regular expressions and the regular expression parser doesn't understand yours.
You must escape the + :
var company = company.replace(/\+/g, "%2B");
You can use this :
var company = company.replace(/\+/g, "%2B");
Or an easier way :
var company = encodeURIComponent(company);
which will do the same operation as the regex one. Also, it encodes all URI chars like &, " (quotes), % etc... if there is one like it in the string given.
In both cases, the output is :
Microst%2BApple%2BGoogle
I'm trying to take some parsed XML data, search it for instances of the tag and replace that tag (and anything that may be inside the font tag), and replace it with a simple tag.
This is how I've been doing my regexes:
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/; //Test against valid email
console.log('regex: ' + emailReg.test(testString));
and so I figured the font regex would be something like this:
var fontReg = /'<'+font+'[^><]*>|<.'+font+'[^><]*>','g'/;
console.log('regex: ' + fontReg.test(testString));
but that isn't working. Anyone know a way to do this? Or what I might be doing wrong in the code above?
I think namuol's answer will serve you better then any RegExp-based solution, but I also think the RegExp deserves some explanation.
JavaScript doesn't allow for interpolation of variable values in RegExp literals.
The quotations become literal character matches and the addition operators become 1-or-more quantifiers. So, your current regex becomes capable of matching these:
# left of the pipe `|`
'<'font'>
'<''''fontttt'>
# right of the pipe `|`
<#'font'>','g'
<#''''fontttttt'>','g'
But, it will not match these:
<font>
</font>
To inject a variable value into a RegExp, you'll need to use the constructor and string concat:
var fontReg = new RegExp('<' + font + '[^><]*>|<.' + font + '[^><]*>', 'g');
On the other hand, if you meant for literal font, then you just needed:
var fontReg = /<font[^><]*>|<.font[^><]*>/g;
Also, each of those can be shortened by using .?, allowing the halves to be combined:
var fontReg = new RegExp('<.?' + font + '[^><]*>', 'g');
var fontReg = /<.?font[^><]*>/g;
If I understand your problem correctly, this should replace all font tags with simple span tags using jQuery:
$('font').replaceWith(function () {
return $('<span>').append($(this).contents());
});
Here's a working fiddle: http://jsfiddle.net/RhLmk/2/
I'm performing this on a string:
var poo = poo
.replace(/[%][<]/g, "'<")
.replace(/[>][%]/g, ">'")
.replace(/[%]\s*[+]/g, "'+")
.replace(/[+]\s*[%]/g, "+'");
Given the similar if these statements, can these regexs be comebined somehow?
No, I don't think so. At least, I suspect for any transformation involving fewer replaces I can come up with a string that your original and the proposed alternative treat differently. However, it may be that the text you're working with wouldn't trigger the differences, and so for practical purposes a shorter transformation would work as well. Depends on the text.
You can simplify it a little bit. You don't need all the range syntax
poo
.replace(/%</g, "'<")
.replace(/>%/g, ">'")
.replace(/%\s*\+/g, "'+")
.replace(/\+\s*%/g, "+'");
Since in either case, the replacement only turns % into ' and removes spaces:
var poo = 'some annoying %< string >% with some % + text + %';
poo = poo.replace(/%<|>%|%\s*\+|\+\s*%/g, function(match) {
return match.replace('%', '\'').replace(/\s/g,'');
});
// "some annoying '< string >' with some ' + text + '"
Although that's not much simpler...
Using lookahead assertions and capturing:
var poo = poo.replace(/%(?=<)|(>)%|%\s*(?=\+)|(\+)\s*%/g, "$1$2'");
Using capturing alone:
var poo = poo.replace(/(>)%|(\+)\s*%|%(<)|%\s*(\+)/g, "$1$2'$3$4");
If JS's RegExp supported lookbehind assertions:
var poo = poo.replace(/%(?=<)|(?<=>)%|%\s*(?=\+)|(?<=\+)\s*%/g, "'");
but it doesn't.