I would like to use create a object that contains regular expressions as the key value. I tried to use the following syntax:
var kv = { /key/g : "value" };
But it fails according JavaScript lint:
SyntaxError: invalid property id
How can I fix it?
Background: The reason why I want to do this is to implement a workaround that fixes wrong unicode in a HTTP API result. I know this is very hackish, but since I have no control over the API server code I think this is the best I can do.
Currently I implemented the mapping by having a keys array and a values array:
function fixUnicode(text) { var result = text; var keys = []; var values = []; keys.push(/é/g); values.push("é"); keys.push(/è/g); values.push("è"); keys.push(/ê/g); values.push("ê"); keys.push(/ë/g); values.push("ë"); keys.push(/à /g); values.push("à"); keys.push(/ä/g); values.push("ä"); keys.push(/â/g); values.push("â"); keys.push(/ù/g); values.push("ù"); keys.push(/û/g); values.push("û"); keys.push(/ü/g); values.push("ü"); keys.push(/ô/g); values.push("ô"); keys.push(/ö/g); values.push("ö"); keys.push(/î/g); values.push("î"); keys.push(/ï/g); values.push("ï"); keys.push(/ç/g); values.push("ç"); for (var i = 0; i < keys.length; ++i) { result = result.replace(keys[i], values[i]); } return result; }
But I want to implement to use a JavaScript object to map keys as values:
function fixUnicode(text) { var result = text; var keys = { /é/g : "é", /è/g : "è" // etc... }; for (var key in keys) { result = result.replace(key, keys[key]); } return result; }
A regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.
RegExp() constructor. The RegExp constructor creates a regular expression object for matching text with a pattern. For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.
The g and i modifiers have these meanings: g = global, match all instances of the pattern in a string, not just one. i = case-insensitive (so, for example, /a/i will match the string "a" or "A" .
Syntax for creating a RegExp object: 1. var txt=new RegExp(pattern,attributes);
This can be done, but not using object literal syntax. You'll need to do it like this:
var kv = {}; kv[/key/g] = "value"; console.log(kv[/key/g]); // "value"
/key/g
in this case, is being toString()
'd to create the key. This is important to know, because it has an effect on key uniqueness. Consider the following: var x = {}, reg = /foo/; x[reg] = 'bar'; console.log(x[reg]); // "bar" console.log(x[reg.toString()]); // "bar" console.log(x['/foo/']); // "bar'
In summary, I'm semi-scared to ask why you need to do this, but assuming you have your reasons, be careful and make sure you understand what is really happening :)
var result = "abcdef", replacements = { "/a/g": "FOO", "/d/i": "BAR" }; for (var key in replacements) { var parts = key.split('/'); result = result.replace(new RegExp(parts[1], parts[2]), replacements[key]); } console.log(result); //FOObcBARef
var result = "abcdef", replacements = [ [/a/g, "FOO"], [/d/i, "BAR"] ]; for (var i = 0, len = replacements.length; i < len; i++) { var replacement = replacements[i]; result = result.replace(replacement[0], replacement[1]); } console.log(result); //FOObcBARef
var result = "abcdef", replacements = [ [/a/g, "FOO"], [/d/i, "BAR"] ], r; while ((r = replacements.shift()) && (result = String.prototype.replace.apply(result, r))) {} console.log(result); //FOObcBARef
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With