javascript正则表达式从字符串中删除特定格式的文本

时间:2016-07-18 13:38:16

标签: javascript jquery

我有一个动态字符串,我想从String中删除特定的格式文本。 (即*号码)

例如://Define screen sizes let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width let screenHeight = screenSize.height // Sizes of centre cards let centreCardWidth = screenWidth/5 let centreCardHeight = (screenWidth/5) / 0.625 let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5) / 2) let centralizeViewVerticleCard = screenHeight / 2 UIView.animateWithDuration(0.25, delay: 0.5, options: [], animations: { centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, centreCard3.frame.size.width, centreCard3.frame.size.height) }, completion: nil)

输出:/1*Region 1/42*Europe/51*Test/100*New Folder/119*New Folder

提前致谢。

3 个答案:

答案 0 :(得分:2)



var str = "/1*Region 1/42*Europe/51*Test/100*New Folder/119*New Folder";

var regex = /(\/\d+\*)/g;

var output = str.replace( regex, '/' );

console.log( output );




答案 1 :(得分:0)

另一种解决方案是搜索一个数字(\d+),然后搜索*(\*)并将其替换为空。

var dynamic="/1*Region 1/42*Europe/51*Test/100*New Folder/119*New Folder";

var edited=dynamic.replace(/\d+\*/g, '');

console.log(edited);

答案 2 :(得分:0)

以下是使用splitjoin的另一种方式:

var str = '/1*Region 1/42*Europe/51*Test/100*New Folder/119*New Folder',
    result = str.split(/\d+\*/).join('');   

console.log(result);