encodeURIComponent escapes all characters except the following: - _ . ! ~ * ' ( )
But is it possible to extend the functionality encode the above special characters as well.
I know i can do something like this:
encodeURIComponent(str).replace(/\(/g, "%28").replace(/\)/g, "%29");
but I want functionality like this, without using additional functions on the encodeURIComponent
encodeURIComponent(str);
You should create your own function.
You should create your own function, really.
If you really know what you're doing, go to step 1.
Don't say I didn't warn you; there be dragons here:
(function() {
var _fn = encodeURIComponent;
window.encodeURIComponent = function(str) {
return _fn(str).replace(/\(/g, "%28").replace(/\)/g, "%29");
};
}());
you can write your custom encoding function
customEncodeURI(str : string){
var iChars = ':",_{}/\\'; // provide all the set of chars that you want
var encodedStr = ''
for (var i = 0; i < str.length; i++) {
if (iChars.indexOf(str.charAt(i)) != -1) {
var hex = (str.charCodeAt(i)).toString(16);
encodedStr += '%' + hex;
}else {
encodedStr += str[i];
}
}
console.log("Final encoded string is "+encodedStr);
console.log("Final decoded string is "+decodeURIComponent(encodedStr));
return encodedStr;
}
Related
How to encode html code or url to this format?:
\u00253Cb\u002522\....
THANK YOU!
To convert URL to this you need to convert your URL (string) to unicode format
you can try following function for it.
// here toUnicode function is attached to string so that it can used further with
//other strings also
String.prototype.toUnicode = function(){
var result = "";
for(var i = 0; i < this.length; i++){
// Assumption: all characters are < 0xffff
result += "\\u" + ("000" + this[i].charCodeAt(0).toString(16)).substr(-4);
}
return result;
};
let name = "john";
name.toUnicode(); // this will return unicodes of strings
And on another side you can get the Unicode URL using window.location.href (which will be in unicode) you can convert back into string using following function.
// this function coverts the unicode to string char by char
// so you need to loop through the string and call this function a
//nd create resultant string
function unicodeToChar(text) {
return text.replace(/\\u[\dA-F]{4}/gi,
function (match) {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
});
}
I am using this (http://coursesweb.net/javascript/convert-bbcode-html-javascript_cs) as my script for parsing BBCode. I have extended the BBCodes that it can process, however I am encountering a problem when a newline immediately follows an opening tag, e.g.
[code]
code....
[/code]
The problem does not occur if the code is 'inline'
[code]code....[/code]`
The regex being used to match what's inside these tags is (.*?) which I know does not match newlines. I have tried ([^\r\n]) to match newlines but this hasn't worked either.
I imagine it's a simple issue but I have little experience with regex so any help would be appreciated
EDIT: this is the full list of regex's I am using
var tokens = {
'URL' : '((?:(?:[a-z][a-z\\d+\\-.]*:\\/{2}(?:(?:[a-z0-9\\-._~\\!$&\'*+,;=:#|]+|%[\\dA-F]{2})+|[0-9.]+|\\[[a-z0-9.]+:[a-z0-9.]+:[a-z0-9.:]+\\])(?::\\d*)?(?:\\/(?:[a-z0-9\\-._~\\!$&\'*+,;=:#|]+|%[\\dA-F]{2})*)*(?:\\?(?:[a-z0-9\\-._~\\!$&\'*+,;=:#\\/?|]+|%[\\dA-F]{2})*)?(?:#(?:[a-z0-9\\-._~\\!$&\'*+,;=:#\\/?|]+|%[\\dA-F]{2})*)?)|(?:www\\.(?:[a-z0-9\\-._~\\!$&\'*+,;=:#|]+|%[\\dA-F]{2})+(?::\\d*)?(?:\\/(?:[a-z0-9\\-._~\\!$&\'*+,;=:#|]+|%[\\dA-F]{2})*)*(?:\\?(?:[a-z0-9\\-._~\\!$&\'*+,;=:#\\/?|]+|%[\\dA-F]{2})*)?(?:#(?:[a-z0-9\\-._~\\!$&\'*+,;=:#\\/?|]+|%[\\dA-F]{2})*)?)))',
'LINK' : '([a-z0-9\-\./]+[^"\' ]*)',
'EMAIL' : '((?:[\\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*(?:[\\w\!\#$\%\'\*\+\-\/\=\?\^\`{\|\}\~]|&)+#(?:(?:(?:(?:(?:[a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(?:\\d{1,3}\.){3}\\d{1,3}(?:\:\\d{1,5})?))',
'TEXT' : '(.*?)',
'SIMPLETEXT' : '([a-zA-Z0-9-+.,_ ]+)',
'INTTEXT' : '([a-zA-Z0-9-+,_. ]+)',
'IDENTIFIER' : '([a-zA-Z0-9-_]+)',
'COLOR' : '([a-z]+|#[0-9abcdef]+)',
'NUMBER' : '([0-9]+)',
'ALL' : '([^\r\n])',
};
EDIT 2: Full JS for matching
var token_match = /{[A-Z_]+[0-9]*}/ig;
var _getRegEx = function(str) {
var matches = str.match(token_match);
var nrmatches = matches.length;
var i = 0;
var replacement = '';
if (nrmatches <= 0) {
return new RegExp(preg_quote(str), 'g'); // no tokens so return the escaped string
}
for(; i < nrmatches; i += 1) {
// Remove {, } and numbers from the token so it can match the
// keys in tokens
var token = matches[i].replace(/[{}0-9]/g, '');
if (tokens[token]) {
// Escape everything before the token
replacement += preg_quote(str.substr(0, str.indexOf(matches[i]))) + tokens[token];
// Remove everything before the end of the token so it can be used
// with the next token. Doing this so that parts can be escaped
str = str.substr(str.indexOf(matches[i]) + matches[i].length);
}
}
replacement += preg_quote(str);
return new RegExp(replacement, 'gi');
};
var _getTpls = function(str) {
var matches = str.match(token_match);
var nrmatches = matches.length;
var i = 0;
var replacement = '';
var positions = {};
var next_position = 0;
if (nrmatches <= 0) {
return str; // no tokens so return the string
}
for(; i < nrmatches; i += 1) {
// Remove {, } and numbers from the token so it can match the
// keys in tokens
var token = matches[i].replace(/[{}0-9]/g, '');
var position;
// figure out what $# to use ($1, $2)
if (positions[matches[i]]) {
position = positions[matches[i]];
} else {
// token doesn't have a position so increment the next position
// and record this token's position
next_position += 1;
position = next_position;
positions[matches[i]] = position;
}
if (tokens[token]) {
replacement += str.substr(0, str.indexOf(matches[i])) + '$' + position;
str = str.substr(str.indexOf(matches[i]) + matches[i].length);
}
}
replacement += str;
return replacement;
};
This does the trick for me: (updated this one too to avoid confusion)
\[code\]([\s\S]*?)\[\/code\]
See regexpal and enter the following:
[code]
code....
[/code]
[code]code.... [/code]
Update:
Fixed the regex to the following and this works in the Chrome Console for me:
/\[code\]([\s\S]*?)\[\/code\]/g.exec("[code]hello world \n[/code]")
JavaScript does not handle multi-line RegExp matches. Instead you have to use the [\s\S] trick described in this SO answer. Perhaps?
/\[code\][\s\S]*\[code\]/
Also RegExps probably isn't the best choice for parsing syntax. It's is extremely over complicated. I would suggest parsing the string and building an Abstract Syntax Tree then rendering the HTML from that.
I'm trying to split an expression string on the characters +*() but I need to only split there if they are not inside brackets. I am struggling to create the correct regex for this task.
Example:
.N.TESTARRAY[INT1+2] + ONLINE * STACKOVERFLOW
I would like this to produce an array such as:
[ ".N.TESTARRAY[INT1+2]" , "ONLINE" , "STACKOVERFLOW" ]
Currently my regex splits on the characters +*()[]
split(/(\+|\*|\[|\]|\(|\))/g)
Because I split on the square brackets as well, I then loop through the array and merge anything inside the brackets. I think this is clunky though and would like to move it all into the regex.
function mergeInnerBrackets(tokens) {
var merged = [];
var depth = 0;
for(var i = 0; i < tokens.length; i++) {
if(tokens[i] == "[") {
depth++;
merged[merged.length-1] += "[";
} else if(tokens[i] == "]") {
depth--;
merged[merged.length-1] += "]";
} else if(depth > 0) {
merged[merged.length-1] += tokens[i];
} else {
merged.push(tokens[i]);
}
}
return merged;
}
I am using JavaScript and need to be able to support back to IE8. I also read something about being able to do this with negative-look behinds but I saw that JavaScript doesn't support that.
Can string also be like .N.TEST[INT1+2] + ON[INT1] * ON[INT2] ? I'd rather use regex like this.
\s*[+*)(](?![^[]*])\s*
If the string can contain more than one pair of [...]
var str = '.N.TEST[INT1+2] + ON[INT1] * ON[INT2]';
console.log(str.split(/\s*[+*)(](?![^[]*])\s*/));
JSFiddle demo
Let's say I have a string like this:
var str = "/abcd/efgh/ijkl/xxx-1/xxx-2";
How do I, using Javascript and/or jQuery, remove the part of str starting with xxx, till the end of str?
str.substring( 0, str.indexOf( "xxx" ) );
Just:
s.substring(0, s.indexOf("xxx"))
A safer version handling invalid input and lack of matching patterns would be:
function trump(str, pattern) {
var trumped = ""; // default return for invalid string and pattern
if (str && str.length) {
trumped = str;
if (pattern && pattern.length) {
var idx = str.indexOf(pattern);
if (idx != -1) {
trumped = str.substring(0, idx);
}
}
}
return (trumped);
}
which you'd call with:
var s = trump("/abcd/efgh/ijkl/xxx-1/xxx-2", "xxx");
Try using string.slice(start, end):
If you know the exact number of characters you want to remove, from your example:
var str = "/abcd/efgh/ijkl/xxx-1/xxx-2";
new_str = str.slice(0, -11);
This would result in str_new == '/abcd/efgh/ijkl/'
Why this is useful:
If the 'xxx' refers to any string (as the OP said), i.e: 'abc', '1k3', etc, and you do not know beforehand what they could be (i.e: Not constant), the accepted answers, as well as most of the others will not work.
Try this:
str.substring(0, str.indexOf("xxx"));
indexOf will find the position of xxx, and substring will cut out the piece you want.
This will take everything from the start of the string to the beginning of xxx.
str.substring(0,str.indexOf("xxx"));
How to globally replace a forward slash in a JavaScript string?
The following would do but only will replace one occurence:
"string".replace('/', 'ForwardSlash');
For a global replacement, or if you prefer regular expressions, you just have to escape the slash:
"string".replace(/\//g, 'ForwardSlash');
Use a regex literal with the g modifier, and escape the forward slash with a backslash so it doesn't clash with the delimiters.
var str = 'some // slashes', replacement = '';
var replaced = str.replace(/\//g, replacement);
You need to wrap the forward slash to avoid cross browser issues or //commenting out.
str = 'this/that and/if';
var newstr = str.replace(/[/]/g, 'ForwardSlash');
Without using regex (though I would only do this if the search string is user input):
var str = 'Hello/ world/ this has two slashes!';
alert(str.split('/').join(',')); // alerts 'Hello, world, this has two slashes!'
Is this what you want?
'string with / in it'.replace(/\//g, '\\');
This has worked for me in turning "//" into just "/".
str.replace(/\/\//g, '/');
Hi a small correction in the above script..
above script skipping the first character when displaying the output.
function stripSlashes(x)
{
var y = "";
for(i = 0; i < x.length; i++)
{
if(x.charAt(i) == "/")
{
y += "";
}
else
{
y+= x.charAt(i);
}
}
return y;
}
This is Christopher Lincolns idea but with correct code:
function replace(str,find,replace){
if (find){
str = str.toString();
var aStr = str.split(find);
for(var i = 0; i < aStr.length; i++) {
if (i > 0){
str = str + replace + aStr[i];
}else{
str = aStr[i];
}
}
}
return str;
}
Example Usage:
var somevariable = replace('//\\\/\/sdfas/\/\/\\\////','\/sdf','replacethis\');
Javascript global string replacement is unecessarily complicated. This function solves that problem. There is probably a small performance impact, but I'm sure its negligable.
Heres an alternative function, looks much cleaner, but is on average about 25 to 20 percent slower than the above function:
function replace(str,find,replace){
if (find){
str = str.toString().split(find).join(replace);
}
return str;
}
var str = '/questions'; // input: "/questions"
while(str.indexOf('/') != -1){
str = str.replace('/', 'http://stackoverflow.com/');
}
alert(str); // output: "http://stackoverflow.com/questions"
The proposed regex /\//g did not work for me; the rest of the line (//g, replacement);) was commented out.
You can create a RegExp object to make it a bit more readable
str.replace(new RegExp('/'), 'foobar');
If you want to replace all of them add the "g" flag
str.replace(new RegExp('/', 'g'), 'foobar');