the string looks something like below:
/1/2/3/4 however I want to replace this with ?1=2&3=4.
I am planning to use REReplace in ColdFusion.
Could you suggest me a regex for this ?I also thought of using loops but stuck either way...
Thanks in Advance
A bit cumbersome without making it more manageable using a loop as #Leigh suggested; but you can use the following on string inputs that contain even occurrences of n/m in the format you described:
var s = "/1/2/3/4/5/6";
s.replace(/^\//,'?').replace(/(\d+)\/(\d+)/g,'$1=$2').replace(/\//g,'&')
// => "?1=2&3=4&5=6"
Related
I have the following string:
"dogs#cats^horses^fish!birds"
How can I get the following array back?
['dogs','#cats','^horses','^fish','!birds']
Essentially I am trying to split the string while keeping the delimeters. I've tried string.match with no avail.
Assuming those are your only separators then you can do this:
var string = "dogs#cats^horses^fish!birds";
string.replace(/(#|\^|!)/g, '|$1').split('|');
We basically add our own separator, in this case | and split it based on that.
This does what you want:
str.match(/((^|[^\w])\w+)/g)
Without more test cases though, it's hard to say how reliable it would be.
This is also assuming a large set of possible delimiters. If it's a small fixed amount, Samer's solution would be a good way to go
My current code is:
var user_pattern = this.settings.tag;
user_pattern = user_pattern.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); // escape regex
var pattern = new RegExp(user_pattern.replace(/%USERNAME%/i, "(\\S+)"), "ig");
Where this.settings.tag is a string such as "[user=%USERNAME%]" or "#%USERNAME%". The code uses pattern.exec(str) to find any username in the corresponding tag and works perfectly fine. For example, if str = "Hello, [user=test]" then pattern.exec(str) will find test.
This works fine, but I want to be able to stop it from matching if the string is wrapped in [nocode][/nocode] tags. For example, if str = "[nocode]Hello, [user=test], how are you?[/nocode]" thenpattern.exec(str)` should not match anything.
I'm not quite sure where to start. I tried using a (?![nocode]) before and after the pattern, but to no avail. Any help would be great.
I would just test if the string starts with [nocode] first:
/^\[nocode\]/.test('[nocode]');
Then simply do not process it.
Maybe filter out [nocode] before trying to find the username(s)?
pattern.exec(str.replace(/\[nocode\](.*)\[\/nocode\]/g,''));
I know this isn't exactly what you asked for because now you have to use two separate regular expressions, however code readability is important too and doing it this way is definitely better in that aspect. Hope this helps 😉
JSFiddle: http://jsfiddle.net/1f485Lda/1/
It's based on this: Regular Expression to get a string between two strings in Javascript
How to extract "51.50431" and "-0.1133" from LatLng(51.50431, -0.1133)
using jquery.
Tried using substring() but not helpful as numbers in LatLng(51.50431, -0.1133) keep on changes in different ranges. Like some time it can come as LatLng(51.50, -0.1).
Any help?
Regular expressions to the rescue:
'LatLng(51.50, -0.1)'.match(/LatLng\(([^,]+),\s*([^)]+)\)/)
// ["LatLng(51.50, -0.1)", "51.50", "-0.1"]
Im allowing numbers, letters, and special characters except for % and _ in my html textbox. I have the pattern /[a-zA-Z0-9!##$^&*()-+=]/. I think its not the best way to do it because I have to list all special characters except the two mentioned. Is there a way in which I don't have to list all special characters and don't include the two mentioned? BTW, Im using javascript regex.
For the demo please see http://jsfiddle.net/ce8Th/
Please help.
There's no need for that complex loop. Just call replace directly on the whole string:
$(this).val(function (i, v) {
return v.replace(/%|_/g, '');
});
Here's your fiddle: http://jsfiddle.net/ce8Th/1/
You could just do the reverse:
/[%_]/
if (pattern.test( ....
It's also nice to not use regex if you don't have to, not that it makes a big difference in this case:
if ("%_".split().indexOf(text.charAt(i)) > -1) {
A white list is always best. I would recommend keeping what you have except adding a length modifier and start and end characters:
/^[a-zA-Z0-9!##$^&*()-+=]+$/
Would I happen to be corrent in guessing that you are using this user input for a MySQL query involving LIKE to search for partial matches?
If so, don't exclude characters. Instead, escape them on the server-side. For instance:
$output = str_replace(Array("%","_"),Array("\\%","\\_"),$input);
I have a string formatted like this:
item_questions_attributes_abc123_id
I'm specifically trying to get the abc123 bit. That string can be any alphanumeric of any case. No special characters or spaces.
I'm using jQuery, though I'm certainly fine with using straight javascript if that's the best solution.
If it's always the 4th part of the string you can use split.
var original = 'item_questions_attributes_abc123_id';
var result = original.split('_')[3];
Try this:
var myArray = myString.split("_");
alert(myArray[3]);
use split method of javascript and then use 2nd last index you will have your required data.