javascript regular expressions with variable content - javascript

I have a working reg expression that does a replace function based on the expression. It works perfect. It finds a specific string based on the beginning of the string and the expression. This is it:
str.replace(/\bevent[0-9]*\=/, "event");
what this does is it changes event=1 to event.
What if event was a variable word? What if I needed to look for conference also?
I have tried:
var type = "conference";
str.replace(/\b/ + type+ /[0-9]*\=/, "conference");
and:
str.replace(/\b/type/[0-9]*\=/, "conference");
neither worked.
how can I pass a javascript string into a regular expression?

Instead of writing a RegEx literal, use a string to create a new RegExp object:
str.replace(new RegExp('\b' + var + '[0-9]'), …)

You can do that with the RegExp Object:
str.replace(RegExp('\b' + reStr + '[0-9]*\='),StrToReplaceWith)

Create a new regex object with your variable.
read this...
http://www.w3schools.com/js/js_obj_regexp.asp

Related

Add span to specific words

I am trying to add a span to a specific word like this:
var value = "eror";
var template = "/(" + value + ")/g";
$("#content").html($("#content").html().replace(template, '<span class="spell_error">$1</span>'));
Here is my fiddle. I tried using a solution I saw here but it does not seem to work. ANy idea why?
Thank you
You're confusing regular expression literals and strings.
Use this to create your regex :
var template = new RegExp("(" + value + ")", 'g');
A regular expression literal is like this :
/(something)/
There's no quote. But as it is a literal, you can't build it with your code, so that's why you must use the RegExp constructor.
A side note : your replacement yould be made lighter and, more importantly, dryer by using the html variant taking a function as callback :
$("#content").html(function(_,html){
return html.replace(template, '<span class="spell_error">$1</span>')
});

jasmine regexp using toContain function

I am using jasmine for testing JavaScript code.
I would like to check the content of render function in this way:
expect(this.view.el.innerHTML).toContain(''+ 'regexp(any text)' +'');
would be possible to pass some parameter as a regular expression?
If yes, how?
I think you would need to use the toMatch matcher which takes a regular expression (toContain expects a string parameter) and build your regular expression by concatenating the fixed and variable strings something like this:
var searchString = ...
expect(innerHTML).toMatch(new RegExp('' + searchString + ''));

Javascript: Convert a String to Regular Expression

I want to convert a string that looks like a regular expression...into a regular expression.
The reason I want to do this is because I am dynamically building a list of keywords to be used in a regular expression. For example, with file extensions I would be supplying a list of acceptable extensions that I want to include in the regex.
var extList = ['jpg','gif','jpg'];
var exp = /^.*\.(extList)$/;
Thanks, any help is appreciated
You'll want to use the RegExp constructor:
var extList = ['jpg','gif','jpg'];
var reg = new RegExp('^.*\\.(' + extList.join('|') + ')$', 'i');
MDC - RegExp
var extList = "jpg gif png".split(' ');
var exp = new RegExp( "\\.(?:"+extList.join("|")+")$", "i" );
Note that:
You need to double-escape backslashes (once for the string, once for the regexp)
You can supply flags to the regex (such as case-insensitive) as strings
You don't need to anchor your particular regex to the start of the string, right?
I turned your parens into a non-capturing group, (?:...), under the assumption that you don't need to capture what the extension is.
Oh, and your original list of extensions contains 'jpg' twice :)
You can use the RegExp object:
var extList = ['jpg','gif','jpg'];
var exp = new RegExp("^.*\\.(" + extList.join("|") + ")$");

Exact replace of string in Javascript

hidValue="javaScript:java";
replaceStr = "java";
resultStr=hidValue.replace("/\b"+replaceStr+"\b/gi","");
resultStr still contains "javaScript:java"
The above code is not replacing the exact string java. But when I change the code and directly pass the value 'java' it's getting replaced correctly i.e
hidValue="javaScript:java";
resultStr=hidValue.replace(/\bjava\b/gi,"");
resultStr contains "javaScript:"
So how should I pass a variable to replace function such that only the exact match is replaced.
The replace-function does not take a string as first argument but a RegExp-object. You may not mix those two up. To create a RexExp-object out of a combined string, use the appropriate constructor:
resultStr=hidValue.replace(new RegExp("\\b"+replaceStr+"\\b","gi"),"");
Note the double backslashes: You want a backslash in your Regular Expression, but a backslash also serves as escape character in the string, so you'll have to double it.
Notice that in one case you're passing a regular expression literal /\bjava\b/gi, and in the other you're passing a string "/\bjava\b/gi". When using a string as the pattern, String.replace will look for that string, it will not treat the pattern as a regular expression.
If you need to make a regular expression using variables, do it like so:
new RegExp("\\b" + replaceStr + "\\b", "gi")
See:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
`let msisdn = '5093240556699'
let isdnWith = numb.msisdn.slice(8,11);
let msisdnNew = msisdn.replace(isdnWith, 'XXX', 'gi');
show 5093240556XXX`

JavaScript replace()

I'm trying to use the replace function in JavaScript and have a question.
strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(/_existing_0/gi,
"_existing_" + "newCounter");
That works.
But I need to have the "0" be a variable.
I've tried:
_ + myVariable +/gi and also tried
_ + 'myVariable' + /gi
Could someone lend a hand with the syntax for this, please. Thank you.
Use a RegExpobject:
var x = "0";
strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(new RegExp("_existing_" + x, "gi"), "existing" + "newCounter");
You need to use a RegExp object. That'll let you use a string literal as the regex, which in turn will let you use variables.
Assuming you mean that you want the zero to be any single-digit number, this should work:
y = x.replace(/_existing_(?=[0-9])/gi, "existing" + "newCounter");
It looks like you're trying to actually build a regex literal with string concatenation - that won't work. You need to use the RegExp() constructor form instead, in order to inject a specific variable into the regex: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp
If you use the RegExp constructor, you can define your pattern using a string like this:
var myregexp = new RegExp(regexstring, "gims") //first param is pattern, 2nd is options
Since it's a string, you can do stuff like:
var myregexp = new RegExp("existing" + variableThatIsZero, "gims")

Categories