JavaScript从特定的字符集开始替换正则表达式

时间:2015-05-20 07:38:00

标签: javascript regex

我想将以y-开头的字词替换为test,例如:

This is a y-abc-def string
This is y-abc

我需要的输出是:

This is a test string
This is test

我试过

inputString.replace(/(?:^|\W)y-(\w+)(?!\w)/g, 'test')

但这没效果。

5 个答案:

答案 0 :(得分:1)

您可以使用:

inputString = inputString.replace(/(^|\s)y-(\S*)(?!\w)/g, '$1test')

RegEx Demo

答案 1 :(得分:0)

inputString.replace(/\by-\S*/g, "test")

答案 2 :(得分:-1)

如果'words'表示import random def createitem(): weapon = ['sword', 'dagger', 'axe'] armor = ['cloth armor', 'chainmail', 'leather armor', 'full plate'] magicpre = ['shining', 'burning', 'aether', 'fine'] magicsuf = ['of the bear', 'of the wolf', 'of the jackal'] magictag = '*MAGIC*' rarepre = ['angelic', 'demonic', 'blessed', 'cursed', 'masterwork'] raresuf = ['of heaven', 'of hell', 'of the king', 'of the chimera'] raretag = '*RARE*' pendingitem = [] itemname = '' appenditem = pendingitem.append(itemname) aq = random.randint(0,5) for num in range(0, aq): weporarm = random.randint(0,1) raritycheck = random.randint(0,5) preorsuf = random.randint(0,1) if weporarm == 1: if raritycheck == 5: itemname = raretag, random.choice(rarepre), random.choice(armor), random.choice(raresuf) elif raritycheck == 4 and preorsuf == 2: itemname = raretag, random.choice(armor), random.choice(raresuf) elif raritycheck == 4 and preorsuf == 1: itemname = raretag, random.choice(rarepre), random.choice(armor) elif raritycheck == 3: itemname = magictag, random.choice(magicpre), random.choice(armor). random.choice(magicsuf) elif raritycheck == 2 and preorsuf == 0: itemname = magictag, random.choice(magicpre), random.choice(armor) elif raritycheck == 2 and preorsuf == 1: itemname = magictag, randcom.choice(armor), random.choice(magicsuf) elif raritycheck == 1 and preorsuf == 0: itemname = magictag, random.choice(magicpre). random.choice(armor) elif raritycheck == 1 and preorsuf == 1: itemname = magictag, random.choice(armor), random.choice(magicsuf) else: itemname = random.choice(armor) else: if raritycheck == 5: itemname = raretag, random.choice(rarepre), random.choice(weapon), random.choice(raresuf) elif raritycheck == 4 and preorsuf == 2: itemname = raretag, random.choice(weapon), random.choice(raresuf) elif raritycheck == 4 and preorsuf == 1: itemname = raretag, random.choice(rarepre), random.choice(weapon) elif raritycheck == 3: itemname = magictag, random.choice(magicpre), random.choice(weapon). random.choice(magicsuf) elif raritycheck == 2 and preorsuf == 0: itemname = magictag, random.choice(magicpre), random.choice(weapon) elif raritycheck == 2 and preorsuf == 1: itemname = magictag, randcom.choice(weapon), random.choice(magicsuf) elif raritycheck == 1 and preorsuf == 0: itemname = magictag, random.choice(magicpre). random.choice(weapon) elif raritycheck == 1 and preorsuf == 1: itemname = magictag, random.choice(weapon), random.choice(magicsuf) else: itemname = random.choice(weapon) appenditem a = str(pendingitem) print('Your inventory:', a) s和连字符组,则应执行以下操作:

\w

即。单词边界,inputString.replace(/\by-[\w-]*/g, 'test') 然后混合使用单词字符和连字符。

答案 3 :(得分:-1)

试试这个:

inputString.replace(/ y\-[\S]+/g, ' test')

关于输入:

> var inputString = "This is a y-abc-def string";
> inputString.replace(/ y\-[\S]+/g, ' test')
< "This is a test string"

答案 4 :(得分:-1)

你可以试试这个。测试如下:

var str = 'y-Thi$s is a y-ab-cdef y-ABC y-*&222 string y-4444';
console.log(str.replace(/(\s)?y-[^\s]+(\s)?/g,'$1test$2'));