RegEx: remove | - javascript

var a="value1%7Cvalue2=%20a%20|value3"
url is encoded in such a way that for some values it is encoded as %7C and some places it is | sign only.
Without decoding this string how to remove everything that comes after first | using regular expression?

Using a regex, as you asked:
var a = "value1%7Cvalue2=%20a%20|value3"
var regex = /\|.*/;
a = a.replace(regex, "");
console.log(a);
We match the | followed by an unlimited number of characters, and replace the match with the empty string.
It's much easier to do with a split, though.
a = a.split('|')[0]
console.log(a)

Why regex? Try split.
var a="value1%7Cvalue2=%20a%20|value3";
var b = a.split('|')[0];

see this demo https://regex101.com/r/dE0jW4/2
/([^|]*)\|.*/
var re = /([^|]*)\|.*/gm;
var str = 'value1%7Cvalue2=%20a%20|value3"';
var subst = '$1';
var result = str.replace(re, subst);

Related

Remove string between two variables

I have a string which has some data with a few special characters, Need to remove the data between the desired special char in JavaScript.
The special char would be obtained in a variable.
var desiredChar = "~0~";
And Imagine this to be the Input string:
~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
So I'm supposed to remove the text in bold.
The desired output is supposed to be-
~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~
I've tried using "Replace" and "Regex", but as the desired character is being passed in a variable and keeps changing I'm facing difficulties.
You can create your own regex based on whatever the bounding character(s) are that contain the text you want removed, and then replace any text that matches that regex with a blank string "".
The JS below should work for your use case (and it should work for multiple occurrences as well):
var originalText = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var desiredChar = "~0~";
var customRegex = new RegExp(desiredChar + ".*?" + desiredChar, "gi");
var processedText = originalText.replace(customRegex, "");
console.log(processedText);
You can build your regex from the constructor with a string input.
var desiredChar = "~0~";
// use the g flag in your regex if you want to remove all substrings between desiredChar
var myRegex = new Regex(desiredChar + ".*" + desiredChar, 'ig');
var testString = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
testString = testString.replace(myRegex, "");
Given input string you can use .indexOf(), .lastIndexOf() and .slice().
Note, OR character | passed to RegExp constructor should be escaped to avoid RegExp created by passing string interpreting | character as OR | within resulting RegExp passed to .replace().
var desiredChar = "~0~";
var str = "~0~1|0|20170807|45|111.00|~0~~1~1|0|20170807|50|666.00|~1~~2~1|0|20170807|55|111.00|~2~";
var not = str.slice(str.indexOf(desiredChar), str.lastIndexOf(desiredChar) + desiredChar.length);
// escape OR `|`
var res = str.replace(new RegExp(not.replace(/[|]/g, "\\|")), "");
console.log(res)
You can use the RegExp object:
var regexstring = "whatever";
var regexp = new RegExp(regexstring, "gi");
var str = "whateverTest";
var str2 = str.replace(regexp, "other");
document.write(str2);
Then you can construct regexstring in any way you want.
You can read more about it at http://www.regular-expressions.info/javascript.html

regex match everything between two different characters

I want to match a string between (but not including) these two characters: ? and &
Example string:
localhost/path/doc.html?970441179&token=specialtoken&actionurl=/portletaction/01654/0112
So from the above I want to match the string 970441179
var str = "?samplestring&";
var patt = /[?]([^&]*)[&]/g;
var res = patt.exec(str)[1];
'res' is your desired result.
Try this regex (\d+)(?=&):
var str = "localhost/path/doc.html?970441179&token=specialtoken&actionurl=/portletaction/01654/0112";
console.log(str.match(/(\d+)(?=&)/g));
Note that it will work just for that specific case.

Split string and get array using regExp in javascript/node js

I am writing js code to get array of elements after splitting using regular expression.
var data = "ABCXYZ88";
var regexp = "([A-Z]{3})([A-Z]{3}d{2})";
console.log(data.split(regexp));
It returns
[ 'ABCXYZ88' ]
But I am expecting something like
['ABC','XYZ','88']
Any thoughts?
I fixed your regex, then matched it against your string and extracted the relevant capturing groups:
var regex = /([A-Z]{3})([A-Z]{3})(\d{2})/g;
var str = 'ABCXYZ88';
let m = regex.exec(str);
if (m !== null) {
console.log(m.slice(1)); // prints ["ABC", "XYZ", "88"]
}
In your case, I don't think you can split using a regex as you were trying, as there don't seem to be any delimiting characters to match against. For this to work, you'd have to have a string like 'ABC|XYZ|88'; then you could do 'ABC|XYZ|88'.split(/\|/g). (Of course, you wouldn't use a regex for such a simple case.)
Your regexp is not a RegExp object but a string.
Your capturing groups are not correct.
String.prototype.split() is not the function you need. What split() does:
var myString = 'Hello World. How are you doing?';
var splits = myString.split(' ', 3);
console.log(splits); // ["Hello", "World.", "How"]
What you need:
var data = 'ABCXYZ88';
var regexp = /^([A-Z]{3})([A-Z]{3})(\d{2})$/;
var match = data.match(regexp);
console.log(match.slice(1)); // ["ABC", "XYZ", "88"]
Try this. I hope this is what you are looking for.
var reg = data.match(/^([A-Z]{3})([A-Z]{3})(\d{2})$/).slice(1);
https://jsfiddle.net/m5pgpkje/1/

How I can replace a underscore with an space inside a word starting with an special char in javascript?

I have this string:
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}";
And I'm trying to replace the ALL the underscores with spaces for any word starting with exclamation sing (!) so the string should looks like this one:
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap Accessible},currentView:!map}";
I spent a few hours trying to figure out how to do that without success.
Try to use function replacement form (example):
str.replace(/!\w+/g, function(x) { return x.replace(/_/g, ' '); })
Regexp /!\w+/g selects all words started with "!". After that we replace each word x with result of x.replace(/_/g, ' ').
You can use the regex
!([^_]+)_([^]+)
and replace with $1 $2
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}";
console.log(str.replace(/!([^_]+)_([^]+)/g, "!$1 $2"));
(![^_]+)_([a-zA-Z0-9]+)
Try this.See demo.
http://regex101.com/r/tF5fT5/57
var re = /(![^_]+)_([a-zA-Z0-9]+)/gm;
var str = '{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}';
var subst = '$1 $2';
var result = str.replace(re, subst);

Regular expression to get numbers from the string in javascript

I have a String like the following
minmaxSize2-8
minmaxSize12-20
How to get the range from the above strings.I need to get 2-8 and 12-20.
Please suggest the regular expressions in javascript
You can do it like this:
var myString = "minmaxSize12-20";
var myRegexp = /(\d+)-(\d+)/g; // Numbers dash Numbers
var match = myRegexp.exec(myString);
alert(match[1]); // 12
alert(match[2]); // 20
Something like this should work:
var str = 'minmaxSize12-20';
var range = str.replace(/^.*?Size/i, ''); // returns 12-20
Simply :
"minmaxSize12-20".match(/(\d+)-(\d+)/)
or even
/(\d+)-(\d+)/.exec("minmaxSize12-20");

Categories

Resources