Omitting special characters from the string using Jquery - javascript

I have to implement some type of pixel for analytic and it requires passing a session Id in the url string. My sessionID contains special characters. It looks something like this BFhGlzT6FBkDr2Zndp0!-1309
I need to remove the (-!) characters from this string, how do I achieve this using jquery? I need to make sure jquery remove those characters before it render otherwise it will not report a visit to analytic.
Guys thanks your help but maybe I need to explain bit further, my pixel code look some what like this "
img src="https://sometime.com/png?org_id=k8vif92&session_
id=T6PtTyRSqhGPYBhp84frwth67n6fL7wcLBFhGlzT6FBkDr2Zndp0!-130901808!1319637471144&m=2&m=2" alt="">
Before this pixel fire off, I need to replace the character in the sessionId string to remove !- and keep in mind session id will change every time there is a new session. I need a code that is generic so it works no matter what session id is, it needs to delete special characters from it.

Try using .replace:
var token = "BFhGlzT6FBkDr2Zndp0!-1309";
token.replace(/[^A-Za-z0-9]/g, "");
this will remove any character that's not a letter or number. More concisely:
token.replace(/\W/g, "");
(this won't replace underscores)

Black-listing ! and - (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[!\-]/g,""));
White-listing alpha-numeric (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[^a-z0-9]/ig,""));

var str = "BFhGlzT6FBkDr2Zndp0!-1309".replace("!-","");
fiddle: http://jsfiddle.net/neilheinrich/eYCjX/

Define a regular expression character set that contains all the allowed characters. For example, yours might look like /[a-zA-Z0-9]/. Now invert it with the complementary character set [^a-zA-Z0-9] and remove all those characters with the String.replace method.
mystring = mystring.replace(/[^a-zA-Z0-9]/g, "");

You dont need jquery for this. You can use javascript regex. See this jsfiddle
var code = "BFhGlzT6FBkDr2Zndp0!-1309";
code = code.replace(/[!-]/g,"");
alert(code);

Related

How do I replace string within quotes in javascript?

I have this in a javascript/jQuery string (This string is grabbed from an html ($('#shortcode')) elements value which could be changed if user clicks some buttons)
[csvtohtml_create include_rows="1-10"
debug_mode="no" source_type="visualizer_plugin" path="map"
source_files="bundeslander_staple.csv" include cols="1,2,4" exclude cols="3"]
In a textbox (named incl_sc) I have the value:
include cols="2,4"
I want to replace include_cols="1,2,4" from the above string with the value from the textbox.
so basically:
How do I replace include_cols values here? (include_cols="2,4" instead of include_cols="1,2,4") I'm great at many things but regex is not one of them. I guess regex is the thing to use here?
I'm trying this:
var s = $('#shortcode').html();
//I want to replace include cols="1,2,4" exclude cols="3"
//with include_cols="1,2" exclude_cols="3" for example
s.replace('/([include="])[^]*?\1/g', incl_sc.val() );
but I don't get any replacement at all (the string s is same string as $("#shortcode").html(). Obviously I'm doing something really dumb. Please help :-)
In short what you will need is
s.replace(/include cols="[^"]+"/g, incl_sc.val());
There were a couple problems with your code,
To use a regex with String.prototype.replace, you must pass a regex as the first argument, but you were actually passing a string.
This is a regex literal /regex/ while this isn't '/actually a string/'
In the text you supplied in your question include_cols is written as include cols (with a space)
And your regex was formed wrong. I recomend testing them in this website, where you can also learn more if you want.
The code above will replace the part include cols="1,2,3" by whatever is in the textarea, regardless of whats between the quotes (as long it doesn't contain another quote).
First of all I think you need to remove the quotes and fix a little bit the regex.
const r = /(include_cols=\")(.*)(\")/g;
s.replace(r, `$1${incl_sc.val()}$3`)
Basically, I group the first and last part in order to include them at the end of the replacement. You can also avoid create the first and last group and put it literally in the last argument of the replace function, like this:
const r = /include_cols=\"(.*)\"/g;
s.replace(r, `include_cols="${incl_sc.val()}"`)

RegExp URL validation

var re = /^(https?:\/\/)?([0-9a-z\.-]+)[^-]\.([a-z]{2,9}\.?)([\/#].\S*)*$/i;
re.test("http://zcontest.ru/dir%201/dir_2/program.ext?var1=x&var2=my%20value");
re.test("http://zcontest.ru/dir%201/dir_2/program.ext&var1=x&var2=my%20value");
Hi guys. What i must add or change, so that parametres in URL cant start with &(ampersand), like in second example ?
I tried to modify your initial regex as little as possible, since it was unclear exactly what your goal was. Here is the regex as you asked:
^(https?:\/\/)?([0-9a-z\.-]+)[^-]\.([a-z]{2,9}\.?)([\/#].[^?\s]*)*\?([^\t \n]*)*$
Also if you are not having a multiline string being checked, then the \n is unnecessary. Additionally note that it will not match strings which do not contain a ?, such as:
http://zcontest.ru/dir%201/dir_2/program.ext

How to replace string in javascript?

I have a string which is like
var test="clientNumber=123,TestSubject=Tom";
I need to replace Tom with Mary. Tom is entered by the user and could be anything but i need to make it Mary while processing . Also clientNumber & TestSubject could be in any order in the string.
Thanks
You seem to want to use a regular expression, for example
test = test.replace(/(TestSubject=)[^,]*/, '$1Mary')
The code I give replaces everything between "TestSubject=" and the end of string or the next comma.
This changes
"clientNumber=123,TestSubject=Tom" into "clientNumber=123,TestSubject=Mary"
and
"TestSubject=Tom,clientNumber=123" into "TestSubject=Mary,clientNumber=123"

Jquery replace a specific character combination

I can't figure out why my jquery replacement code wouldn't work.
I try to make the follow replacement:
http://mysite.com/_thumbs/0000312/0312718/0312718_$varm.jpg
I added '$var' to the position where I want to put a number, we can take '1' for now.
So I need to replace $var for 1.
What I tried;
var img = $('img', this).attr('src'); // I grabs the image url like above.
img.replace(/$var/, 1)
But nothing happens.
Thanks in advance!
Nick
The dollar sign is a special character in regular expressions.
img.replace(/\$var/, "1");
Escaping the $ with a backslash will tell JavaScript that you want it to match a dollar sign. Otherwise, $ means "match the end of the search string".
edit — also note that if you want the updated string you'll need to save the return value from calling .replace():
img = img.replace(/\$var/, "1");
(You can of course save the replacement results in a different variable.)

Get part of string?

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.

Categories

Resources